locked
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

Answers

  • User3866881 posted

    Hi:)

    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">
        <title>Test</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:HiddenField ID="HiddenField1" runat="server" />
            <asp:GridView ID="GridView1" runat="server" EnableModelValidation="True" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
                <Columns>
                    <asp:CommandField HeaderText="Select" ShowSelectButton="True" />
                </Columns>
            </asp:GridView>
        </div>
        <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" />
            </p>
        </asp:Panel>
        <asp:ModalPopupExtender ID="Panel1_ModalPopupExtender" runat="server" DynamicServicePath=""
            Enabled="True" TargetControlID="HiddenField1" PopupControlID="Panel1">
        </asp:ModalPopupExtender>
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        </form>
    </body>
    </html>

    【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 };
                    GridView1.DataBind();
                }
            }

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

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

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