2013-02-07 30 views

回答

17

實際上存在GridViewCommandEventArgs沒有行,所以你需要從命令源獲取該行命名容器

GridViewRow row = (GridViewRow)(((Control)e.CommandSource).NamingContainer); 

,那麼你將能夠使用

TextBox myTextBox = row.FindControl("MyTextBoxId") as TextBox; 

希望這有助於!

+0

Control is undefined? –

0

如果妳想要找到在命令行使用控制

controlname controlId=(controlname)e.FindControl("controlId"); 

例如如果u想找到一個拉布勒 ID爲LBL然後用..

Label lbl = (Label)e.Row.FindControl("lbl"); 
1
GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer); 

Label lblProdId = (Label)row.FindControl(「lblproductId」); 
0

您可以使用「CommandName」在控件中使用「CommandArgument」。 這2個參數:

<asp:LinkButton ID="UpdateButton" runat="server" CommandName="Update" CommandArgument='<%# Container.DataItemIndex + ";" + Eval("idinterlocuteur") %>'> 

然後在後面的代碼,你可以得到的參數:

string[] arg = e.CommandArgument.ToString().Split(';'); 
int index = Convert.ToInt16(arg[0]); 
string idinterlocuteur = arg[1]; 

現在你的論點找到你的控件:

CheckBox Check1 = GridView1.Rows[index].FindControl("MyCheckboxinrow") as CheckBox; 
0

如果您使用的用戶控件在你的gridview itemtemplate中,然後((Control)e.CommandSource).NamingContainer可能不會返回你的gridviewrow。

在這種情況下,我用下面的代碼獲取當前行:

var c = ((Control) e.CommandSource).NamingContainer; 
while (c.GetType() != typeof(GridViewRow)) 
{ 
    c = c.Parent; 
} 
var currentRow = (GridViewRow) c; 

這不是很漂亮,但它的工作原理。

2

如果使用的LinkBut​​ton

LinkButton ctrl = e.CommandSource as LinkButton; 
    if (ctrl != null) 
    { 
     GridViewRow row = ctrl.Parent.NamingContainer as GridViewRow; 
     TextBox txtDescription = (TextBox)row.FindControl("txtDescription"); 
    } 
0
<asp:TemplateField HeaderText="Next Date To Attend" ItemStyle-CssClass="col-md-2" > 
      <EditItemTemplate> 
       <asp:TextBox ID="NextAttendTextBox" CssClass="col-sm-12" runat="server"></asp:TextBox> 
       <span class="text-muted">DD-MM-YYYY</span> 
      </EditItemTemplate> 
      <ItemTemplate> 
       <%#Eval("NextAttend") %> 
      </ItemTemplate> 
      <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" /> 
     </asp:TemplateField> 

     <asp:TemplateField HeaderText="Update Status" ItemStyle-CssClass="col-md-1" > 
      <EditItemTemplate> 
       <div class="btn-group"> 

       <asp:LinkButton ID="LinkButton31" class="btn btn-sm btn-success" CommandArgument='<%#Container.DataItemIndex %>' CommandName="UpdateStat" runat="server" > 
        <i class="ace-icon fa fa-save"></i></asp:LinkButton> 
       <asp:LinkButton ID="LinkButton32" class="btn btn-sm btn-error" CommandName="Cancel" runat="server" > 
        <i class="ace-icon fa fa-close"></i></asp:LinkButton> 
        </div> 
      </EditItemTemplate> 
      <ItemTemplate> 
       <div class="btn-group"> 
       <asp:LinkButton ID="LinkButton3" class="btn btn-sm btn-warning" CommandName="Edit" runat="server" > 
        <i class="ace-icon fa fa-upload"></i></asp:LinkButton> 

        </div> 
      </ItemTemplate> 
      <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" /> 
     </asp:TemplateField> 


if (e.CommandName == "UpdateStat") 
     { 
      HiddenField IDHiddenField=(HiddenField)GridView1.Rows[Convert.ToInt32(e.CommandArgument)].FindControl("IDHiddenField"); 
      TextBox CurrentStatDesTextBox=(TextBox)GridView1.Rows[Convert.ToInt32(e.CommandArgument)].FindControl("CurrentStatDesTextBox");} 
+1

請提供您的代碼的一些信息/細節 – jhhoff02

0

GridViewRow GVR =(GridViewRow)((對照)e.CommandSource).NamingContainer; int rowIndex = gvr.RowIndex;

 string Cat_name = (GridView1.Rows[rowIndex].FindControl("TxtName") as TextBox).Text; 
0
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)  
{ 

GridViewRow gvr = (GridViewRow)((Control)e.CommandSource).NamingContainer; 
int rowIndex = gvr.RowIndex; 

string Cat_name = (GridView1.Rows[rowIndex].FindControl("TxtName") as TextBox).Text; 

}