如何从gridview(更新面板之间包含)读取值到文本框?

时间:2014-04-21 06:51:40

标签: c# asp.net ajax

我有4个文本框,即first_name,last_name,gender和department以及一个gridview。 GridView包含编辑和删除按钮。当用户单击编辑按钮时,gridview中的值将读入文本框,用户将更新它,但是当我使用更新面板仅刷新gridview而不是整个页面时,这将不起作用。值无法从gridview读取到文本框。我只希望gridview视图不刷新整个页面。这是我的代码。

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs"            Inherits="Task2._Default" %>

    <!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></title>
     <style type="text/css">
        .style2
        {
            width: 208px;
        }
        .style3
        {
            width: 81px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table style="width: 53%;">
        <tr>
            <td class="style3">
                First Name</td>
            <td class="style2">
                <asp:TextBox ID="TextBox_FirstName" runat="server" Width="230px"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="style3">
                Last Name</td>
            <td class="style2">
                <asp:TextBox ID="TextBox_LastName" runat="server" Width="230px"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="style3">
                Gender</td>
            <td class="style2">
                <asp:TextBox ID="TextBox_Gender" runat="server" Width="230px" MaxLength="1"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="style3">
                Department</td>
            <td class="style2">
                <asp:TextBox ID="TextBox_Department" runat="server" Width="230px"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td align="center" class="style3" colspan="1">
                &nbsp;</td>
            <td>
                    <asp:Button ID="Button_Save" runat="server" Text="Save" Width="107px" 
                        onclick="Button_Save_Click"/>
                    <asp:Button ID="Button_Update" runat="server" style="margin-left: 14px" 
                        Text="Update" Width="104px" onclick="Button_Update_Click" />
                </td>
            </tr>
        </table>
    </div>
    <br />
    <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <asp:UpdatePanel ID="UpdatePanel_GridOne" runat="server" UpdateMode="Conditional">
            <ContentTemplate>
         <asp:GridView ID="GridOne" runat="server" AutoGenerateColumns="False"
                        EmptyDataText="No Records Found" Width="419px" >
            <Columns>
                            <asp:TemplateField HeaderText="First Name">
                                <ItemTemplate>
                                    <asp:Label ID="First_Name" runat="server" Text='<%#Eval("First_Name") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Last Name">
                                <ItemTemplate>
                                    <asp:Label ID="Last_Name" runat="server" Text='<%#Eval("Last_Name") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Gender">
                                <ItemTemplate>
                                    <asp:Label ID="Gender" runat="server" Text='<%#Eval("Gender") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Department">
                                <ItemTemplate>
                                    <asp:Label ID="Department" runat="server" Text='<%#Eval("Department") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Action">
                                <ItemTemplate>
                                    <asp:Button ID="Edit" runat="server" Text="Edit" OnClick="edit_button"/>
                                    <asp:Button ID="Delete" runat="server" Text="Delete" OnClientClick="return confirm('Are you sure? want to delete the record.');"  OnClick="delete_button"/>
                                    <asp:Label ID="EmployeeID" runat="server" Text='<%#Eval("EmployeeID") %>' Visible="false"></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>           
                     </Columns>
        </asp:GridView>
        <input type="hidden" runat="server" id="hidEmployeeID" />
        </ContentTemplate>
        </asp:UpdatePanel>
      </div>
    </form>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

您还可以在另一个更新面板内覆盖gridview外的文本框..

<asp:UpdatePanel ID="UpdatePanel2 runat="server" >
<ContentTemplate>
    <asp:TextBox ID="TextBox_LastName" runat="server" Width="230px"></asp:TextBox>
 </ContentTemplate>
 </asp:UpdatePanel>