gridview在点击事件后不显示更新数据

时间:2014-03-14 10:15:58

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

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

CODE:

<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;
    }

2 个答案:

答案 0 :(得分:1)

搜索数据后,您应该将网格的数据源设置为来自搜索的新数据

例如

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

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

答案 1 :(得分:0)

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