Pass modalpopup datakey back into gridview RRS feed

  • Question

  • User-367758494 posted

    How can I pass a gridview row from a modalpopup back into the Gridview which called it, and have that Gridview row selected? 

    Thank you.

    Sunday, April 17, 2011 7:45 AM


  • User3866881 posted


    Try this please:

    【aspx Codes】

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="TestWebApp.WebForm1" %>

    <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <form id="form1" runat="server">
            <asp:HiddenField ID="HiddenField1" runat="server" />
            <asp:GridView ID="GridView1" runat="server" EnableModelValidation="True" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
                    <asp:CommandField HeaderText="Select" ShowSelectButton="True" />
        <asp:Panel ID="Panel1" runat="server" Height="160px" Width="238px">
            <p align="center">
                Click here to test<asp:Button ID="Button1" runat="server" Text="Click Me" OnClick="Button1_Click" />
        <asp:ModalPopupExtender ID="Panel1_ModalPopupExtender" runat="server" DynamicServicePath=""
            Enabled="True" TargetControlID="HiddenField1" PopupControlID="Panel1">
        <asp:ScriptManager ID="ScriptManager1" runat="server">

    【CS codes】

    namespace TestWebApp
        public partial class WebForm1 : System.Web.UI.Page
            protected void Page_Load(object sender, EventArgs e)
                if (!IsPostBack)
                    GridView1.DataSource = new int[] {1,2,3,4,5 };

            protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
                ViewState["sid"] = GridView1.SelectedIndex;

            protected void Button1_Click(object sender, EventArgs e)
                GridView1.Rows[(int)ViewState["sid"]].BackColor = Color.Red;

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 19, 2011 10:39 PM