GridView同列控制兩個Button
這個練習是在小舖內看到的討論串...按了那一個GridView1中的其中一個 BUTTON 如何抓取
不過最終的問題好像是.....發問者沒把問題點指出來吧!!(個人感覺)
小舖內的高手也很有耐心的把新手問題指引出來.....
動手練習才發現,我也卡在一個簡單卻不起眼的地方...
就是GridView1_RowCreated................因GridView1_RowCommand的selectindex 點第一列的按鈕時是 –1
當點第二列按紐時既然變成 0,除非連按兩次才會正常。
參考的文章有以下幾篇
[習題] "選取" Gridview的某一列 & 那一列對應資料表的P.K.值 好書每次看感覺都不一樣....
如何獲得GridView內LinkButton的RowIndex......後來寫這個紀錄才找到這篇
介紹了1.0 DataGrid1 與 2.0 寫法,2.0的寫法一行就解決我的問題了。
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Select") {
int rowIndex = ((GridViewRow)((LinkButton)e.CommandSource).NamingContainer).RowIndex;
Label1.Text = GridView1.DataKeys[rowIndex].Value.ToString();
}
高手的筆記拿來記一下好了....
e.CommandSource傳的是按下去的LinkButton,不過由於傳回的是Object,就得自行轉成LinkButton,但由於我們想知道的是RowIndex,而LinkButton是包含在GridViewRow內,所以透過NamingContainer傳回目前的GridViewRow,但傳回的是Control,所以需在轉成GridViewRow後才能有RowIndex property。
才發現我的寫法因該是屬於比較笨的方式,每次點擊按鈕後,進去RowCreated建立表單
並把CommandArgument 塞進去..也不知道這樣到底好不好提出來跟大家討論與分享..............
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="SqlDataSource1"
onselectedindexchanged="GridView1_SelectedIndexChanged"
onselectedindexchanging="GridView1_SelectedIndexChanging"
onrowcommand="GridView1_RowCommand" EnableModelValidation="False"
onrowcreated="GridView1_RowCreated">
<Columns>
<asp:BoundField DataField="bookid" HeaderText="bookid"
SortExpression="bookid" />
<asp:BoundField DataField="book_name" HeaderText="book_name"
SortExpression="book_name" />
<asp:BoundField DataField="book_author" HeaderText="book_author"
SortExpression="book_author" />
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="Button1" runat="server" CommandName="Select" Text="Button1"
onclick="Button1_Click" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="Button2" runat="server" CommandName="Select" Text="Button2"
onclick="Button2_Click" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:DA_DATABASEConnectionString %>"
SelectCommand="SELECT * FROM [book]"></asp:SqlDataSource>
</div>
</form>
</body>
</html>
後台頁
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
int bu_flag; //RowCommand_bu_flag button的指標
protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
Response.Write("------------------------</br>");
string get_text = GridView1.Rows[e.NewSelectedIndex].Cells[2].Text.ToString();
Button btn = (Button)GridView1.Rows[e.NewSelectedIndex].FindControl("Button1");
Button btn2 = (Button)GridView1.Rows[e.NewSelectedIndex].FindControl("Button2");
if (btn != null && bu_flag == 0)
{
Response.Write("GridView1_SelectedIndexChanging() </br>");
Response.Write("button1選擇後取值=" + get_text + "</br>");
}
if (btn2 != null && bu_flag == 1)
{
Response.Write("GridView1_SelectedIndexChanging() </br>");
Response.Write("button2選擇後取值=" + get_text + "</br>");
}
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
Response.Write("------------------------</br>");
string get_text = GridView1.SelectedRow.Cells[2].Text.ToString();
GridViewRow row = GridView1.SelectedRow;
Button btn = (Button)row.FindControl("Button1");
Button btn2 = (Button)row.FindControl("Button2");
if (btn != null && bu_flag == 0)
{
Response.Write("GridView1_SelectedIndexChanged() </br>");
Response.Write("button1選擇後取值=" + get_text +"</br>");
}
if (btn2 != null && bu_flag == 1)
{
Response.Write("GridView1_SelectedIndexChanged() </br>");
Response.Write("button2選擇後取值=" + get_text + "</br>");
}
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
//int select_index_flag = ((GridViewRow)((Button)e.CommandSource).NamingContainer).RowIndex; //查詢別人提的寫法2.0
int select_index_flag = Convert.ToInt32(e.CommandArgument);
string get_text = GridView1.Rows[select_index_flag].Cells[2].Text.ToString();
if (bu_flag == 0)
{
Response.Write("GridView1_RowCommand()</br>");
Response.Write("button1選擇後取值=" + get_text + "</br>");
}
if (bu_flag == 1)
{
Response.Write("GridView1_RowCommand() </br>");
Response.Write("button2選擇後取值=" + get_text + "</br>");
}
}
protected void Button1_Click(object sender, EventArgs e)
{
bu_flag = 0;
}
protected void Button2_Click(object sender, EventArgs e)
{
bu_flag = 1;
}
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
Button btn = new Button();
if (e.Row.RowType == DataControlRowType.DataRow)
{
btn = (Button)e.Row.FindControl("Button1");
if (btn != null)
btn.CommandArgument = e.Row.RowIndex.ToString();
}
Button btn2 = new Button();
if (e.Row.RowType == DataControlRowType.DataRow)
{
btn2 = (Button)e.Row.FindControl("Button2");
if (btn2 != null)
btn2.CommandArgument = e.Row.RowIndex.ToString();
}
}
}
心得:
寫完這篇之後.........我覺得我因該好好把MIS2000大大的專題實務..好好的在複習過一遍了.....
另外補充精靈果然不能常用,不然會腦智商會退化................會變成虛有其表....再來就會被書中提到的內功震死.......
大家一起加入blogads 賺零用錢!!