如何在网格视图的行命令中找到控件?

时间:2013-02-07 10:19:59

标签: c# asp.net .net vb.net visual-studio

如何在网格视图的行命令中找到控件?

11 个答案:

答案 0 :(得分:17)

实际上GridViewCommandEventArgs中没有Row,因此您需要从命令源命名容器中获取行

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

然后你就可以使用

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

希望这有帮助!

答案 1 :(得分:2)

如果您使用LinkBut​​ton

private void Download(object sender, TappedRoutedEventArgs e)
        {
            // Create a WriteableBitmap with height and width same as that of Layoutroot
            WriteableBitmap bmp = new WriteableBitmap(480, 696);

            //Render the layoutroot element on it
            bmp.Render(Scrshot, null);
            bmp.Invalidate();


            //Save the image to Medialibrary

            //create a stream of image
            var ms = new MemoryStream();
            bmp.SaveJpeg(ms, 480, 696, 0, 100);
            ms.Seek(0, SeekOrigin.Begin);


            //every path must be unique
            var filePath = "myfile" + DateTime.Now.ToString();

            //Remember to include Medialib capabilty from WMAppManifest.xml for acessing the Medialibrary
            var lib = new MediaLibrary();
            lib.SavePicture(filePath, ms);

            MessageBox.Show("Saved in your media library!", "Done", MessageBoxButton.OK);
        }

答案 2 :(得分:1)

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

Label lblProdId = (Label)row.FindControl(“lblproductId”);

答案 3 :(得分:0)

如果你想在行命令中找到一个控件使用

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

例如,如果您想找到ID lbl 标签,请使用..

Label lbl = (Label)e.Row.FindControl("lbl");

答案 4 :(得分:0)

您可以使用" CommandArgument"在你的控制中使用" CommandName"。 这里有两个论点:

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;

现在你争论找到你的控件:

{
  “kind”: “clms#course”,
  “id”: long,
  “course-name”: string,
  “course-icon”: string,
  “product-name”: string,
  “product-icon”: string,
  “_links”: {
    “self”: {“href” : string}, 
    “unitlist”: {“href” : string} // This is a link to list of units for the course.
  }
}

答案 5 :(得分:0)

如果您在gridview itemtemplate中使用usercontrols,那么((Control)e.CommandSource).NamingContainer可能不会返回您的gridviewrow。

在这种情况下,我使用以下代码获取当前行:

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

它不漂亮,但它确实有效。

答案 6 :(得分: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");}

答案 7 :(得分:0)

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

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

答案 8 :(得分: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;

}

答案 9 :(得分:0)

 GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);                    
                    HiddenField hdMeasurementId = ((HiddenField)row.FindControl("hdMeasurementId"));

答案 10 :(得分:0)

GridViewCommandEventArgs不支持该行,因此请使用命名容器来查找控件。

  protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
       Control ctrl = e.CommandSource as Control;  
       if (ctrl != null)
       {
           GridViewRow gvRow = ctrl.Parent.NamingContainer as GridViewRow; 
           Label slno = (Label)gvRow.FindControl("slno");  // Find Your Control here
           TextBox txtno = (TextBox)gvRow.FindControl("txtno");  // Find Your Control here
           // Your work start here
       }
    }