将girdview文本框的值设置为按钮的命令参数

本文关键字:按钮 命令 参数 设置 girdview 文本 | 更新日期: 2023-09-27 18:06:40

这是我的网格视图

<asp:GridView ID="grvClaimBanks" runat="server" AutoGenerateColumns="false" AllowPaging="false" OnRowCommand="grvClaimBanks_RowCommand">
<Columns>
 <asp:TemplateField HeaderText="Sr.No">
  <ItemTemplate>
    <%#Container.DataItemIndex +1  %>
  </ItemTemplate>                                            
 </asp:TemplateField>
 <asp:TemplateField HeaderText="ID">
  <ItemTemplate>
    <asp:TextBox ID="txtMoratoriumPeroid" runat="server"> 
  </ItemTemplate>                                            
 </asp:TemplateField>
 <asp:TemplateField HeaderText="Action">
  <ItemTemplate>
   <asp:LinkButton ID="lnkEditProduction" runat="server" CommandArgument='<%# Eval("TextileSLACId") %>' CommandName="EnterClaim" Text="Enter Claim" CausesValidation="false"></asp:LinkButton>
  </ItemTemplate>                                           
 </asp:TemplateField>
</Columns>

现在我想把"txtMoratoriumPeroid"的值设置为"lnkEditProduction"的"commandparameter",这样我就可以在网格视图的行命令事件中访问该值。

这可能吗?

将girdview文本框的值设置为按钮的命令参数

您绑定TextBox的方式不会在客户端给您值变化,而您的TextBox在服务器端似乎为空。您可以在RowCommand事件中获得当前命令按钮的row,并找到TextBox以获取其值。

GridViewRow row = (GridViewRow)(((Control)e.CommandSource).NamingContainer);
TextBox txtMoratoriumPeroid= row.FindControl("txtMoratoriumPeroid") as TextBox;
if(txtMoratoriumPeroid != null)
{
    //Your code here.
    string txtMoratoriumPeroidText =  txtMoratoriumPeroid.Text; 
}

不,不可能动态地给出CommandArgument。但是你可以通过

来实现
LinkButton lbtnSender = (LinkButton)sender;
TextBox txtMoratoriumPeroid = (TextBox)lbtnSender.Parent.Parent.FindControl("txtMoratoriumPeroid");
string MoratoriumPeroid = txtMoratoriumPeroid.Text;