简体   繁体   中英

how to call a function on click on a gridview row. both functions are placed in script tags in aspx page

I want to be able to call a function when i click on gridview row. how to do it

this is where i handle click on gridview row

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

here is the function i wanna call

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

    }
}

This Should be your code:

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

Referance Link:

ASP.NET gridview row onclick

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

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

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

        }

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM