[英]Get Selected Row On Button Click
我有一個執行以下操作的GridView:
protected override void Render(System.Web.UI.HtmlTextWriter textWriter)
{
foreach (GridViewRow gvRow in gvNotifications.Rows)
{
if (gvRow.RowType == DataControlRowType.DataRow)
{
gvRow.Attributes.Add("onmouseover", "this.previous_color=this.style.backgroundColor;this.style.backgroundColor='#FFFF99';this.style.cursor='hand';");
gvRow.Attributes.Add("onmouseout", "this.style.backgroundColor=this.previous_color;");
gvRow.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(gvNotifications, "Select$" + gvRow.RowIndex,true);
}
}
base.Render(textWriter);
}
protected void gvNotifications_SelectedIndexChanged(object sender, EventArgs e)
{
gvNotifications.SelectedRowStyle.BackColor = Color.LightBlue;
}
我有一個名為btnTest的按鈕,我希望能夠將當前行轉換為我的自定義對象。 我已經嘗試了以下方法,但沒有任何運氣。
Customer currentRow = (Customer)gvNotifications.SelectedRow.DataItem;
“ DataItem”始終為null。 我敢肯定這是一個簡單的解決方法,但是在谷歌搜索問題之后,我還沒有找到任何可行的方法。
這是我在aspx中的GridView:
<asp:GridView
ID="gvNotifications"
runat="server"
AutoGenerateColumns="false"
GridLines="None"
CssClass="mGrid"
AlternatingRowStyle-BackColor="#F0F0F0"
AllowPaging="true"
AllowSorting="true"
PagerStyle-CssClass="pgr"
PageSize="25"
OnPageIndexChanging="gvNotifications_PageIndexChanging"
OnSorting="gvNotifications_Sorted" HeaderStyle-CssClass="srtAc"
onselectedindexchanged="gvNotifications_SelectedIndexChanged">
<Columns>
<asp:TemplateField HeaderText="Customer Id" SortExpression="CustomerId" HeaderStyle-ForeColor="WhiteSmoke">
<ItemTemplate>
<%#Eval("CustomerId")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Contact Name" SortExpression="ContactName" HeaderStyle-ForeColor="WhiteSmoke">
<ItemTemplate>
<%#Eval("ContactName")%>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="btnTest" runat="server" Text="Use" onclick="btnTest_Click" />
嘗試此操作以獲取選定的行
protected void YourGridView_SelectedIndexChanged(object sender, EventArgs e)
{
//Grab the selected row
GridViewRow row = YourGridViewId.SelectedRow;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.