2

我希望能够在单击 gridview 行时调用一个函数。怎么做

这是我处理点击gridview行的地方

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{

  if (e.Row.RowType == DataControlRowType.DataRow)
   {
 // somthing like
  // e.Row.Attributes["onclick"] = go to Myfunction;
//OR
 //  e.Row.Attributes.Add("onclick", Myfunction);
   }
}

这是我想调用的函数

 protected void Myfunction(object sender, EventArgs e)
{
     int id = 0;

foreach (GridViewRow myrow in GridView1.Rows)
{
    RadioButton btn= (RadioButton)row.FindControl("RadioButton1");
    if (btn.Checked)
    { 
    id = Convert.ToInt32(GridView1.Rows[row.RowIndex].Cells[1].Text);

    }
}
4

4 回答 4

0
protected void rdb_select_CheckedChanged(object sender, EventArgs e)
        {
            RadioButton selectButton = (RadioButton)sender;
            GridViewRow row = (GridViewRow)selectButton.Parent.Parent;
            int a = row.RowIndex;
            foreach (GridViewRow rw in GridView1.Rows)
            {
                if (selectButton.Checked)
                {
                    if (rw.RowIndex != a)
                    {
                        RadioButton rd = rw.FindControl("rdb_select") as RadioButton;
                        rd.Checked = false;
                    }
                }
            }

        }
于 2013-06-03T08:40:50.450 回答
0
e.Row.Attributes.Add("onclick", Myfunction);

需要一个名为 Myfunction 的 javascript 函数,但您的“Myfunction”似乎是一个 C# 方法。

于 2013-04-27T09:15:19.423 回答
0

If u r using listbox with update pannel so,try this,.its working..

protected void DDLInclusions_SelectedIndexChanged(object sender, EventArgs e)
{
        string name = "";
        ListBox selectedValues = (ListBox)sender;
        GridViewRow row = (GridViewRow)selectedValues.NamingContainer;
        int a = row.RowIndex;
        foreach (GridViewRow rw in gvwRoomType.Rows)
        {
            if (rw.RowIndex == a)
            {
                ListBox lstRoomInclusions = rw.FindControl("DDLInclusions") as ListBox;
                for (int i = 0; i < lstRoomInclusions.Items.Count; i++)
                {
                    if (lstRoomInclusions.Items[i].Selected)
                    {
                        name += lstRoomInclusions.Items[i].Text + ",";
                    }
                }
                TextBox txtInclusions = rw.FindControl("txtRoomInclusions") as TextBox;
                txtInclusions.Text = name;
            }
        }
}
于 2013-10-19T11:41:22.370 回答
0

这应该是你的代码:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{ 
   if (e.Row.RowType == DataControlRowType.DataRow)
   {
        //Add onclick attribute to select row.
        e.Row.Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference(YourFunction, "Select$" + e.Row.RowIndex.ToString()));
   }
}

参考链接:

ASP.NET gridview 行 onclick

于 2013-04-27T08:44:58.470 回答