<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
onselectedindexchanged="GridView1_SelectedIndexChanged"
onselectedindexchanging="GridView1_SelectedIndexChanging">
<Columns>
<asp:CommandField HeaderText="Show" SelectText="ShowMessage"
ShowSelectButton="True" />
</Columns>
</asp:GridView>
<div id="content"&开发者_运维百科gt;<div>
protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
string html = "";
for (int i = 0; i < GridView1.Rows.Count; i++)
{
html += "<div style='display:none' id='" + i + "'>" + GridView1.Rows[i].Cells[3].Text + "</div>";
GridView1.Rows[i].Cells[0].Attributes.Add("onclick", "$('#" + i + "').dialog()");
}
content.InnerHtml = html;
}
show dialog onclick cells[0] and automatically close dialog? why?
My WebForms is somewhat rusty but if that GridView1_SelectedIndexChanging
is going to be called each time you change to another row the code will be recreating the dialog bit each time. That could be why it disappears.
Whenever you're selecting another row in your grid, the GridView1_SelectedIndexChanging
event on server needs to be fired, for that a complete PostBack
needs to occur in order to get the changed data.
Whenever, a PostBack
occurs, all your things done using JavaScript are gone and need to be initialize once again when the page is loaded again.
Consider a PostBack
like a new page request to the server.
You may consider using ASP.NET AJAX and put your DataGrid
in an UpdatePanel
to avoid full page PostBack
but only the DataGrid section.
精彩评论