简体   繁体   English

更新面板gridview asp.net C#

[英]Update panel gridview asp.net C#

Can you help me with this. 你能帮我吗 When I click on ID 1,2.... take the ID from query string and display those names where id from query string is equal on id_proba in the other table. 当我单击ID 1,2 ....时,从查询字符串中获取ID,然后在另一张表中的id_proba上显示那些来自查询字符串的ID相等的名称。 This is ok and show me. 可以,告诉我。 Now i like when i click on ID 1,2.. in GridView1 to refresh only gridview 2 not all page. 现在我喜欢单击GridView1中的ID 1,2 ..以仅刷新gridview 2而不是所有页面。 Need gridview to put in Ajax Update Panel. 需要gridview才能放入Ajax更新面板。 I put gridview2 in Update panel but don't know what to write in code? 我将gridview2放在“更新”面板中,但不知道该用什么写代码?

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
    DataKeyNames="id" DataSourceID="SqlDataSource1" CellPadding="4" 
    ForeColor="#333333" GridLines="None" 
    onselectedindexchanged="GridView1_SelectedIndexChanged">
    <AlternatingRowStyle BackColor="White" />
    <Columns>
        <asp:CommandField ShowSelectButton="True" />
        <asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />

        <asp:HyperLinkField 
  DataTextField="id" 
  DataTextFormatString=" {0}" 
  DataNavigateUrlFields="id" HeaderText="id"
  DataNavigateUrlFormatString="WebForm1.aspx?ID={0}"   />
    </Columns>
    <EditRowStyle BackColor="#2461BF" />
    <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
    <RowStyle BackColor="#EFF3FB" />
    <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
    <SortedAscendingCellStyle BackColor="#F5F7FB" />
    <SortedAscendingHeaderStyle BackColor="#6D95E1" />
    <SortedDescendingCellStyle BackColor="#E9EBEF" />
    <SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:ProbaConnectionString %>" 
    DeleteCommand="DELETE FROM [Proba1] WHERE [id] = @id" 
    InsertCommand="INSERT INTO [Proba1] ([name]) VALUES (@name)" 
    SelectCommand="SELECT [name], [id] FROM [Proba1]" 
    UpdateCommand="UPDATE [Proba1] SET [name] = @name WHERE [id] = @id">
    <DeleteParameters>
        <asp:Parameter Name="id" Type="Int32" />
    </DeleteParameters>
    <InsertParameters>
        <asp:Parameter Name="name" Type="String" />
    </InsertParameters>
    <UpdateParameters>
        <asp:Parameter Name="name" Type="String" />
        <asp:Parameter Name="id" Type="Int32" />
    </UpdateParameters>
</asp:SqlDataSource>
<br />
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
        <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" 
            DataSourceID="SqlDataSource2">
            <Columns>
                <asp:BoundField DataField="id_proba" HeaderText="id_proba" 
                    SortExpression="id_proba" />
                <asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
            </Columns>
        </asp:GridView>
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
            ConnectionString="<%$ ConnectionStrings:ProbaConnectionString %>" 
            SelectCommand="SELECT [id_proba], [name] FROM [proba3] WHERE ([id_proba] = @id_proba)">
            <SelectParameters>
                <asp:QueryStringParameter Name="id_proba" QueryStringField="ID" Type="Int32" />
            </SelectParameters>
        </asp:SqlDataSource>
    </ContentTemplate>
</asp:UpdatePanel>

To refresh the update panel by clicking in GridView1 you could add a javascript onclick handler to the links in GirdView1 . 要通过单击GridView1刷新更新面板,可以在GirdView1的链接中添加一个javascript onclick处理程序。
In this event handler you could refresh the update panel by calling __doPostBack . 在此事件处理程序中,您可以通过调用__doPostBack刷新更新面板。 You could use jQuery to add the eventhandler to all a tags in the grid as follows: 您可以使用jQuery将事件处理程序添加到网格中的所有标签,如下所示:

<script>
   $(function() {
         $("#<%=GridView1.ClientID%> a").bind('click', function() {
             refreshUpdatePanel();
          });
   });

  function refreshUpdatePanel()
  {
    __doPostBack('UpdatePanel1', '');
  }
</script>

If your grids are not too big, put them both in the same update panel. 如果您的网格不太大,请将它们放在同一更新面板中。
That way you will not have to write any code to get GridView2 update on clicks in GridView1 . 这样,你就不必编写任何代码来获得GridView2在点击更新GridView1

Like this: 像这样:

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
       <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            DataKeyNames="id" DataSourceID="SqlDataSource1" CellPadding="4" 
            ForeColor="#333333" GridLines="None" 
            onselectedindexchanged="GridView1_SelectedIndexChanged">
            <AlternatingRowStyle BackColor="White" />
            <Columns>
                <asp:CommandField ShowSelectButton="True" />
                <asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />

                <asp:HyperLinkField 
          DataTextField="id" 
          DataTextFormatString=" {0}" 
          DataNavigateUrlFields="id" HeaderText="id"
          DataNavigateUrlFormatString="WebForm1.aspx?ID={0}"   />
            </Columns>
            <EditRowStyle BackColor="#2461BF" />
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#EFF3FB" />
            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
            <SortedAscendingCellStyle BackColor="#F5F7FB" />
            <SortedAscendingHeaderStyle BackColor="#6D95E1" />
            <SortedDescendingCellStyle BackColor="#E9EBEF" />
            <SortedDescendingHeaderStyle BackColor="#4870BE" />
        </asp:GridView>

        <br />
        <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" 
            DataSourceID="SqlDataSource2">
            <Columns>
                <asp:BoundField DataField="id_proba" HeaderText="id_proba" 
                    SortExpression="id_proba" />
                <asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
            </Columns>
        </asp:GridView>
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
            ConnectionString="<%$ ConnectionStrings:ProbaConnectionString %>" 
            SelectCommand="SELECT [id_proba], [name] FROM [proba3] WHERE ([id_proba] = @id_proba)">
            <SelectParameters>
                <asp:QueryStringParameter Name="id_proba" QueryStringField="ID" Type="Int32" />
            </SelectParameters>
        </asp:SqlDataSource>
    </ContentTemplate>
</asp:UpdatePanel>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:ProbaConnectionString %>" 
    DeleteCommand="DELETE FROM [Proba1] WHERE [id] = @id" 
    InsertCommand="INSERT INTO [Proba1] ([name]) VALUES (@name)" 
    SelectCommand="SELECT [name], [id] FROM [Proba1]" 
    UpdateCommand="UPDATE [Proba1] SET [name] = @name WHERE [id] = @id">
    <DeleteParameters>
        <asp:Parameter Name="id" Type="Int32" />
    </DeleteParameters>
    <InsertParameters>
        <asp:Parameter Name="name" Type="String" />
    </InsertParameters>
    <UpdateParameters>
        <asp:Parameter Name="name" Type="String" />
        <asp:Parameter Name="id" Type="Int32" />
    </UpdateParameters>
</asp:SqlDataSource>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM