如何在单击 gridview 行时调用函数。这两个函数都放在aspx页面的脚本标签中

问题描述 投票:0回答:4

我希望能够在单击 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);

    }
}
c# asp.net gridview
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;
                    }
                }
            }

        }

0
投票

如果您使用带有更新面板的列表框,请尝试这个,它工作正常..

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;
            }
        }
}

0
投票
e.Row.Attributes.Add("onclick", Myfunction);

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


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

© www.soinside.com 2019 - 2024. All rights reserved.