模态弹出窗口内的Gridview

时间:2010-11-12 11:56:50

标签: c# ajax gridview

我写了一个显示gridview的modelpopup。 gridview加载和数据完全显示现在我想选择一行,它应该将值传递给页面中的几个文本框,从那里打开弹出窗口。

// Default.aspx的

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeFile="Default.aspx.cs" Inherits="_Default" %>

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
   <asp:ScriptManager ID="ScriptManager1" runat="server" EnableScriptGlobalization="true"
        EnableScriptLocalization="true">
    </asp:ScriptManager>
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
    <asp:Button ID="btnclear" runat="server" CssClass="LPS_Button" Text="Clear" OnClick="btnclear_Click" />
    <asp:Button ID="btnShowPopup" runat="server" Style="display: none" />
    <cc1:ModalPopupExtender ID="mdlPopup" runat="server" TargetControlID="btnShowPopup"
        PopupControlID="pnlCities" CancelControlID="btnClose" BackgroundCssClass="modalBackground">
    </cc1:ModalPopupExtender>
    <asp:Panel ID="pnlCities" runat="server" Style="background-color: White; border-color: Black;
        border-style: solid; border-width: thin;" SkinID="PopUpPanel">
        <asp:UpdatePanel ID="upCities" runat="server" UpdateMode="Conditional">
            <ContentTemplate>
                <asp:Label ID="lblCities" runat="server" Text="Search Result" SkinID="Heading"></asp:Label><br />
                <asp:GridView ID="gvsearch" CssClass="LPS_DataGrid" runat="server" AllowPaging="True"
                    AutoGenerateColumns="False" CellPadding="4" GridLines="None" Height="100%" OnPageIndexChanging="gvsearch_PageIndexChanging"
                    PageSize="5" ShowFooter="True" Width="100%" OnRowCommand="gvsearch_RowCommand">
                    <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                    <Columns>
                        <asp:TemplateField HeaderText="ID">
                            <ItemTemplate>
                                <asp:HyperLink ID="lnkid" runat="server" Text='<%# Bind("ID") %>'></asp:HyperLink>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Name">
                            <ItemTemplate>
                                <asp:HyperLink ID="lnkname" runat="server" Text='<%# Bind("Name") %>'></asp:HyperLink>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Select">
                            <ItemTemplate>
                                <asp:Button ID="btgselect" runat="server" Text="Select" CommandName="Select" CommandArgument='<%# Eval("ID") %>' />
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                    <RowStyle />
                    <FooterStyle CssClass="LPS_Grid_FooterStyle" />
                    <PagerStyle CssClass="LPS_Grid_PageStyle" />
                    <SelectedRowStyle CssClass="LPS_Grid_SelectedRowStyle" />
                    <HeaderStyle CssClass="LPS_Grid_HeaderStyle" />
                </asp:GridView>
            </ContentTemplate>
        </asp:UpdatePanel>
        <br />
        <div style="text-align: center; width: 100%; margin-top: 5px;">
            <asp:Button ID="btnClose" runat="server" Text="Close" Width="50px" />
        </div>
    </asp:Panel>
</asp:Content>

// default.aspx.cs coding

protected void btnclear_Click(object sender, EventArgs e)
{
    popup();
    //Session["tablename"] = "Vendor";
    //Response.Write("<script language='javascript'>window.open('Search.aspx',null,'height=450, width=450,status= no, resizable= no, scrollbars=yes, toolbar=no,location=no,menubar=no ');</script>");        
}
protected void gvsearch_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    popup();
    gvsearch.PageIndex = e.NewPageIndex;
    gvsearch.DataBind();
}
void popup()
{
    DataSet ds = new DataSet();
    DataTable dt = new DataTable();
    popupsearch pop = new popupsearch();
    XmlDocument xmlsearch = new XmlDocument();
    XmlElement xmlroot = xmlsearch.CreateElement("Search");
    XmlElement xmlsearchs = xmlsearch.CreateElement("Popup");
    xmlsearchs.SetAttribute("tablename", "employee");
    xmlroot.AppendChild(xmlsearchs);
    xmlsearch.AppendChild(xmlroot);
    ds = pop.search(xmlsearch);
    dt = ds.Tables.Add("list");
    dt.Columns.Add("ID", Type.GetType("System.String"));
    dt.Columns.Add("Name", Type.GetType("System.String"));
    DataRow myRow;
    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
    {
        if (ds.Tables[0].Rows.Count > 0)
        {
            myRow = dt.NewRow();
            myRow["ID"] = ds.Tables[0].Rows[i][0].ToString();
            myRow["Name"] = ds.Tables[0].Rows[i][1].ToString();
            dt.Rows.Add(myRow);
        }
    }
    gvsearch.DataSource = ds.Tables["list"];
    gvsearch.DataBind();
    upCities.Update();
    mdlPopup.Show();
}
protected void gvsearch_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "Select")
    {
        TextBox1.Text = e.CommandArgument.ToString();
    }
}
请朋友们帮帮我。

1 个答案:

答案 0 :(得分:2)

将您的文本框放在UpdatePanel属性设置为Always的{​​{3}}内:

<asp:UpdatePanel ID="upTextBoxes" runat="server" UpdateMode="Always">
    <ContentTemplate>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
    </ContentTemplate>
</asp:UpdatePanel>

这样,当网格视图执行异步回发时,您的文本框将被刷新。

编辑:如果您还想关闭模式弹出窗口,请在更新文本框时执行该服务器端:

protected void gvsearch_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "Select") {
        TextBox1.Text = e.CommandArgument.ToString();
        mdlPopup.Hide();
    }
}