Sunday, March 11, 2012

how can catch select event of a gridview which is in an updatepanel?

ProtectedSub GridView1_SelectedIndexChanged(ByVal senderAsObject,ByVal eAs System.EventArgs)Handles GridView1.SelectedIndexChanged
ScriptManager.RegisterClientScriptBlock(...)
EndSub


Hi,

Do it like this:

protected void Page_Load(object sender, EventArgs e)
{
//Attach a Javascript funtion to the LinkButton.
LinkButton myLinkButton;
for (int i = 0; i < GridView1.Rows.Count; i++)
{
myLinkButton = (LinkButton)GridView1.Rows[i].Cells[4].FindControl("LinkButton1");
myLinkButton.Attributes.Add("onclick", "shopModalPopup('" + GridView1.Rows[i].Cells[0].Text + "');return false;");
}
}

or:

protected void Page_Load(object sender, EventArgs e)
{
//Attach a Javascript funtion to the GridViewRow.
GridViewRow myGridViewRow;
for (int i = 0; i < GridView1.Rows.Count; i++)
{
myGridViewRow = (GridViewRow)GridView1.Rows[i];
myGridViewRow.Attributes.Add("onclick", "shopModalPopup('" + GridView1.Rows[i].Cells[0].Text + "');return false;");
}
}

For more information,seehttp://forums.asp.net/p/1159886/1915998.aspx#1915998

Best Regards,


Just change the "myLinkButton" in my code to your select button and change the "shopModalPopup" function to your "client javascript function".

Happy coding:)


Thank you this worked for me as well, much appreciated.

There is one minor issue for me, I have to click twice on the Select button on the gridview for my javascript function to fire, any suggestions?

Dim myGridViewRowAs GridViewRow

For iAsInteger = 0To GridView1.Rows.Count - 1

myGridViewRow =DirectCast(GridView1.Rows(i), GridViewRow)

myGridViewRow.Attributes.Add("onclick","DoAlert('" + GridView1.Rows(i).Cells(0).Text +"');return false;")

Next


Move it out of the Page_Load event and into the GridView1_RowDataBound event. This provides simpler code and better encapsulation of logic.

Protected Sub GridView1_RowDataBound(ByVal senderAs Object,ByVal eAs System.Web.UI.WebControls.GridViewRowEventArgs)Handles GridView1.RowDataBoundIf (e.Row.RowType = DataControlRowType.DataRow)Then e.Row.Attributes.Add("onclick", "DoAlert('" & e.Row.Cells(0).Text & "');return false;")End If End Sub


Actually it wasn't in the Page_Load but in the GridView1_Selected index. Yes moving it to the Row_DataBound does infact work as you described. In fix this though, now my values aren't getting assigned (see code below) properly. It seems that the code in the RowDataBound happens before the Selected IndexChanged. Any thoughts?

Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.SelectedIndexChanged
TextBox2.Text = GridView1.Rows(GridView1.SelectedIndex).Cells(1).Text
TextBox3.Text = GridView1.Rows(GridView1.SelectedIndex).Cells(2).Text
End Sub

Thank you...


Nevermind, got it fixed. Thanks for you suggestion worked like a charm!!!! Big Smile


Sorry, I assumed you had it in Page_Load because that's where Jin-Yu Yin suggested you place it.

Are the two textboxes within an UpdatePanel?

No comments:

Post a Comment