English 中文(简体)
如何在所选的下拉列表中获取行索引?
原标题:How to get row index in Dropdownlist selectedIndexChanged?
  • 时间:2012-05-24 19:50:19
  •  标签:
  • c#
  • asp.net

我用的是电网视图和Sqldatasource

我的网格里有一个下限列表,有两个值:是和否。

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    GridViewRow row = GridView1.Rows[e.RowIndex];
    DropDownList ddl = ((DropDownList)row.FindControl("DropdownList1"));
    if(ddl.selectedvalue == "1")
        //etc..
}

我需要获得行索引, 因为当前事件没有 < code> GridViewRow 行 = GridView1. Rows[e. RowIndex]; 。

最佳回答

@mellamokb已经提到, 你总是能控制发件人的论点引发的事件,

DropDownList ddl = (DropDownList)sender;

如果您还需要参考 DropDownList (或GridView的模板字段中的任何其他控件) 的 GridViewRow (或GridView的模板字段中的任何其他控件), 您可以使用 < a href=" http://msdn.microft.com/en- us/library/system.web.ui. contr. control. namingcontainer.aspx"\\\code> NamingContaner 属性。

GridViewRow row = (GridViewRow)ddl.NamingContainer;

但我需要从一个模板字段中获取一个数值的行索引,该模板字段不是下拉键,而是一个文本框。

一旦您有 GridViewRow 引用, 您可以通过 row. find control ("ID") (模板字段) 或 row.Cells[index]. controls[0] (圆形字段) 获得任何控制。

例如(假设在另一列中有 < code> TextBox ):

TextBox txtName = (TextBox)row.FindControl("TxtName");
问题回答

如果您所寻找的只是下拉列表的值, 那通过为 < code> sender 的 s 值 :

DropDownList ddl = sender as DropDownList;
if (ddl.SelectedValue == "1")
   // do something...
Protected Sub ddlneedlocationcmf_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim gvrow As GridViewRow = CType(sender, DropDownList).NamingContainer
    Dim rowindex As Integer = CType(gvrow, GridViewRow).RowIndex


End Sub




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签