![](/img/trans.png)
[英]ASP.NET: How to get values from a selected row from GridView control
[英]how to get control id from gridview in asp.net?
我需要網格視圖中的控件ID才能使用觸發器。
我的代碼在這里:
<asp:GridView ID="gvDetails" CssClass="table table-striped table-bordered datatables dataTable" DataKeyNames="folder_path" CellPadding="5" runat="server" AutoGenerateColumns="false" Width="100%">
<Columns>
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="attachment_name" HeaderText="Attachment" />
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkDownload" Text="Download" runat="server" OnClick="DownloadFile"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderStyle-CssClass="hideGridColumn" ItemStyle-CssClass="hideGridColumn">
<ItemTemplate>
<asp:HiddenField ID="hdnAttach_Id" Value='<%#(Eval("attachment_id").ToString())%>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle BackColor="#2FBDF1" Font-Bold="true" />
</asp:GridView>
我需要類似...的代碼
<Triggers>
<asp:PostBackTrigger ControlID="lnkDownload" />
</Triggers>
如何從gridview獲取“ lnkDownload” ID?
例外情況:
您需要將每個LinkButton
注冊為PostBackTrigger
。 在GridView
綁定了每一行之后,您需要搜索LinkButton
並通過如下代碼進行注冊:
protected void gvDetails_RowDataBound(object sender, GridViewRowEventArgs e)
{
LinkButton lb = e.Row.FindControl("lnkDownload") as LinkButton;
ScriptManager.GetCurrent(this).RegisterPostBackControl(lb);
}
並且需要在RowDataBound
事件上調用它。
您可以使用findcontrol
方法從網格視圖訪問控件
foreach(GridViewRow row in gvDetails.Rows)
{
if(row.RowType == DataControlRowType.DataRow)
{
LinkButton linkButton = (LinkButton )row.FindControl("lnkDownload");
//Your other code
}
}
我最近也遇到了這個問題。 您可以通過執行e.row.findcontrol(“ NameOfControl”)找到一個控件。
由於我不完全了解您打算做什么,因此可以通過找到控件來檢索ClientID。 然后,您可以通過說出Button.ClientID來指定按鈕的ID。 這將顯示ContentHolder1_gridview_0_button_0。
要添加屬性,可以執行button.Attributes.Add(“ attribute”,“#” + button.ClientID);
以下代碼是我已修復的事情,已將以下屬性添加到按鈕。 這樣,我可以單擊一個按鈕並復制文本框。 例:
protected void gvListInventoryPassword_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
HtmlButton buttonPass= (HtmlButton)e.Row.FindControl("buttonPass");
TextBox txtBox= (TextBox)e.Row.FindControl("txtBox");
buttonPass.Attributes.Add("data-clipboard-target", "#" + txtBox.ClientID);
}
}//End of gvListInventoryPassword_RowDataBound function
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.