【问题标题】:gridview doesn't show update data after click event点击事件后gridview不显示更新数据
【发布时间】:2014-04-19 13:44:22
【问题描述】:

我已经用存储过程填充了gridview,它需要registrationNo 参数,因此TEXTBOX 是控制参数,并且该存储过程位于服务器的数据库中,该数据库从平板设备接收新记录。当我在文本框中输入数据后单击搜索按钮时,它会显示新记录,但问题是如果从平板电脑接收到新数据并且我点击搜索按钮然后它不会显示新数据,我必须回到主页然后这个页面再次输入数据并点击搜索按钮,然后显示为什么?

代码:

<form id="form1" runat="server">
    <div>
        <asp:Button ID="btnHome" runat="server" Text="Home" CssClass="button" Width="7%" OnClick="btnHome_Click" />
    </div>
    <br />
    <div>
       <asp:Panel ID="pnlInput" runat="server" DefaultButton="btnSearch">
        <asp:TextBox ID="txtboxVehicleNo" runat="server"></asp:TextBox> &nbsp
        <asp:Button ID="btnSearch" Text="Search" Width="9%" CssClass="button" runat="server" OnClick="btnSearch_Click1" />
       </asp:Panel>
    </div>
    <br />
    <br />
    <div>
        <asp:GridView ID="gvVehicleLedger" ShowHeaderWhenEmpty="True" runat="server" CellPadding="7" DataSourceID="SqlDataSourceETTVehicleLed" ForeColor="#333333" GridLines="None" AllowPaging="True" AutoGenerateColumns="False" DataKeyNames="Vehicle No" Width="100%" BorderColor="#CCCCCC" BorderStyle="Solid" BorderWidth="1px">
            <AlternatingRowStyle BackColor="White" />
            <Columns>
                <asp:BoundField DataField="Vehicle No" HeaderText="Vehicle No" ReadOnly="True" SortExpression="Vehicle No" >
                <HeaderStyle HorizontalAlign="Left" Wrap="False" />
                <ItemStyle HorizontalAlign="Left" />
                </asp:BoundField>
                <asp:BoundField DataField="Transaction Date" HeaderText="Transaction Date" SortExpression="Transaction Date">
                <HeaderStyle HorizontalAlign="Left" />
                <ItemStyle HorizontalAlign="Left" Wrap="False" />
                </asp:BoundField>
                <asp:BoundField DataField="Engine Capacity" HeaderText="Engine Capacity" SortExpression="Engine Capacity" >
                <HeaderStyle HorizontalAlign="Left" Wrap="False" />
                <ItemStyle HorizontalAlign="Left" Wrap="False" />
                </asp:BoundField>
                <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" >
                <HeaderStyle HorizontalAlign="Left" />
                <ItemStyle HorizontalAlign="Left" />
                </asp:BoundField>
                <asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" >
                <HeaderStyle HorizontalAlign="Left" />
                <ItemStyle HorizontalAlign="Left" />
                </asp:BoundField>
                <asp:BoundField DataField="No Of Months" HeaderText="No Of Months" SortExpression="No Of Months" >
                <HeaderStyle HorizontalAlign="Center" Wrap="False" />
                <ItemStyle HorizontalAlign="Center" />
                </asp:BoundField>
                <asp:BoundField DataField="Amount" HeaderText="Amount" SortExpression="Amount" >
                <HeaderStyle HorizontalAlign="Right" />
                <ItemStyle HorizontalAlign="Right" />
                </asp:BoundField>
            </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" />
            <emptydatarowstyle backcolor="#eff3fb"
              forecolor="Red"/>
            <EmptyDataTemplate>
              No Records Found 
            </EmptyDataTemplate> 
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSourceETTVehicleLed" runat="server" ConnectionString="<%$ ConnectionStrings:ETTConnectionStr %>" SelectCommand="Vehicleledger" SelectCommandType="StoredProcedure">
            <SelectParameters>
                <asp:ControlParameter ControlID="txtboxVehicleNo" Name="RegistrationNo" PropertyName="Text" Type="String" />
            </SelectParameters>
        </asp:SqlDataSource>
    </div>

    </form>

.cs代码:

if(!IsPostBack)
        {
            gvVehicleLedger.Visible = false;
        }

protected void btnSearch_Click1(object sender, EventArgs e)
    {
        gvVehicleLedger.Visible = true;
    }

【问题讨论】:

  • 为什么不能正常工作?为什么重定向到页面后它可以工作,为什么不点击搜索按钮?

标签: asp.net .net c#-4.0 gridview webforms


【解决方案1】:

搜索完数据后,你应该设置网格的数据源,即从搜索中获取的新数据

例子

private void serach_click()
{
    // your code to retrieve your data

    gridview.datasource = your data source;
    gridview.databind();
}

【讨论】:

  • 对不起什么?不要得到你
【解决方案2】:

看起来您没有在 searchClick 上刷新 gridView 的数据。我认为你应该使用像 gvVehicleLedger.databind() 这样的 smthng。 (有一个关于这个话题的博客here

【讨论】:

  • 我不明白那个“硬编码”的东西。你已经这样做了:protected void btnSearch_Click1(object sender, EventArgs e) { gvVehicleLedger.Visible = true;因此,您也可以毫无问题地使用它: protected void btnSearch_Click1(object sender, EventArgs e) { gvVehicleLedger.datasource = yourDataSource; gvVehicleLedger.databind(); gvVehicleLedger.Visible = true;还是我错过了什么? yourDataSource 可以是一个变量,因此无论如何它都不是硬编码的。在这种情况下,唯一硬编码的是数据视图的结构。
猜你喜欢
  • 2019-07-29
  • 1970-01-01
  • 2021-04-05
  • 1970-01-01
  • 2011-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多