【问题标题】:GridView, Add a new row dynamically grid and sql database by getting value from two textbox outside gridviewGridView,通过从gridview外部的两个文本框获取值来动态添加新行grid和sql数据库
【发布时间】:2016-09-13 06:45:55
【问题描述】:

我有gridview 和gridview 以上我有两个文本框。当我填充文本框并单击添加按钮时,内容应动态添加到gridview和sql数据库中,当我单击gridview每一行中显示的编辑按钮时,我应该将值返回到上述文本框,添加应该更改为更新按钮,在我更新 gridview 后必须再次填充更新的数据。

这是我的 HTML 页面设计

<body>
   <form id="form1" runat="server">
        <asp:ScriptManager ID="script1" runat="server"></asp:ScriptManager>
    <div>

        <table align="center" style="width:50%;">
            <tr>
                <td class="auto-style1">
                    <asp:Label ID="Label1" runat="server" Text="Country Name" Font-Bold="True" ForeColor="Red"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="Text1" runat="server" Width="180px"></asp:TextBox>
                </td>
                <td>&nbsp;</td>
            </tr>
            <tr>
                <td class="auto-style1">
                    <asp:Label ID="Label2" runat="server" Text="Country Notes" Font-Bold="True" ForeColor="Red"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="Text2" runat="server" Width="180px"></asp:TextBox>
                </td>
                <td>&nbsp;</td>
            </tr>
            <tr>
                <td>
                    <br />
                    <asp:Button ID="Button1" runat="server" Text="Add" BackColor="#990000" ForeColor="White" OnClick="Button1_Click" />
                </td>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
            </tr>

        </table>

        <br />
        <br />

    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
               <Triggers>

                    <asp:AsyncPostBackTrigger ControlID="GridView1" EventName="PageIndexChanging" />

                </Triggers>
        <ContentTemplate>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3" DataKeyNames="CountryID" DataSourceID="SqlDataSource1" OnRowEditing="cmd = new SqlCommand(&quot;insert into country1 (Name,CountryNotes) values(@Name, @CountryNotes)&quot;, con);"  >
            <Columns>

                <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" ShowSelectButton="True" />

                <asp:BoundField DataField="CountryID" HeaderText="CountryID" ReadOnly="True" SortExpression="CountryID" InsertVisible="False" />
                <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
                <asp:BoundField DataField="CountryNotes" HeaderText="CountryNotes" SortExpression="CountryNotes" />
            </Columns>
            <FooterStyle BackColor="White" ForeColor="#000066" />
            <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
            <RowStyle ForeColor="#000066" />
            <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
            <SortedAscendingCellStyle BackColor="#F1F1F1" />
            <SortedAscendingHeaderStyle BackColor="#007DBB" />
            <SortedDescendingCellStyle BackColor="#CAC9C9" />
            <SortedDescendingHeaderStyle BackColor="#00547E" />
        </asp:GridView>
            </ContentTemplate>
    </asp:UpdatePanel>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ATSConnectionString %>" DeleteCommand="DELETE FROM [country1] WHERE [CountryID] = @original_CountryID AND (([Name] = @original_Name) OR ([Name] IS NULL AND @original_Name IS NULL)) AND (([CountryNotes] = @original_CountryNotes) OR ([CountryNotes] IS NULL AND @original_CountryNotes IS NULL))" InsertCommand="INSERT INTO [country1] ([Name], [CountryNotes]) VALUES (@Name, @CountryNotes)" SelectCommand="SELECT * FROM [country1]" UpdateCommand="UPDATE [country1] SET [Name] = @Name, [CountryNotes] = @CountryNotes WHERE [CountryID] = @original_CountryID AND (([Name] = @original_Name) OR ([Name] IS NULL AND @original_Name IS NULL)) AND (([CountryNotes] = @original_CountryNotes) OR ([CountryNotes] IS NULL AND @original_CountryNotes IS NULL))" ConflictDetection="CompareAllValues" OldValuesParameterFormatString="original_{0}">
            <DeleteParameters>
                <asp:Parameter Name="original_CountryID" Type="Int32" />
                <asp:Parameter Name="original_Name" Type="String" />
                <asp:Parameter Name="original_CountryNotes" Type="String" />
            </DeleteParameters>
            <InsertParameters>
                <asp:Parameter Name="Name" Type="String" />
                <asp:Parameter Name="CountryNotes" Type="String" />
            </InsertParameters>
            <UpdateParameters>
                <asp:Parameter Name="Name" Type="String" />
                <asp:Parameter Name="CountryNotes" Type="String" />
                <asp:Parameter Name="original_CountryID" Type="Int32" />
                <asp:Parameter Name="original_Name" Type="String" />
                <asp:Parameter Name="original_CountryNotes" Type="String" />
            </UpdateParameters>
        </asp:SqlDataSource>

    </div>
    </form>
</body>
</html>

【问题讨论】:

  • 你的问题是?
  • 我在 gridview 本身中提供了编辑选项,当我单击编辑选项时,gridview 内的行中的值本身就是在那里编辑的,但我希望这些值回到我用来添加的上面的文本框中,, ,并且在我修改内容后,我必须从那里更新

标签: sql asp.net gridview


【解决方案1】:

您需要在 OnRowEditing 方法中处理此问题。

protected void OnRowEditing(object sender, GridViewEditEventArgs e)
{
   e.NewEditIndex -- will get you the Index of gridview, store this in some session or some variable .Helps you while you updating
   //Assign value to the textbox.
   Text1.text=GridView1.Rows[e.NewEditIndex].Cells[0].Text 
   Text2.text=GridView1.Rows[e.NewEditIndex].Cells[1].Text 
}

并且在 'Add' 按钮事件中检查 EditIndex 是否为 NULL

public void Button1_Click(object sender, System.EventArgs e)
{
  If (EditIndex is null)
  /// DO Insert
  Else
  // Do Update with EditIndex
}

【讨论】:

  • 您遇到任何错误。?在这里分享你的代码。让我们尝试解决它。​​
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-07-27
  • 2015-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-19
  • 1970-01-01
相关资源
最近更新 更多