从网格视图标题获取文本框值

时间:2018-10-04 07:52:13

标签: c# asp.net gridview

我有一个带有文本框的网格,用于在标题上过滤数据。我在网格外部有一个按钮。我正在使用此gridview按某些列过滤网格。当我单击按钮时,我想从codebehind.cs上的文本框和下拉列表中获取价值,以下是我的gridview的webform.aspx代码,

JAX-WS 2.2.6

我正在代码隐藏代码中获得texbox的值。

<asp:Button ID="btngetLocationDate" runat="server" Text="Get Filtered Data" OnClick="getTextBoxValue"></asp:Button>

<asp:GridView ID="TrackerGrid" runat="server" BackColor="#CCCCCC" AllowPaging="True" AutoGenerateColumns="false">
    <AlternatingRowStyle HorizontalAlign="Center" VerticalAlign="Middle" />

    <Columns>
        <asp:TemplateField HeaderText="User ID" SortExpression="User ID">
            <ItemTemplate>
                <asp:Label ID="lbluserid" runat="server" Text='<%# Eval("User ID") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Customer Name">
            <HeaderTemplate>
                Full Name:
               <asp:DropDownList ID="ddlfullName" runat="server" AutoPostBack="true" OnSelectedIndexChanged="onselectDropdown">
                   <asp:ListItem Text="ALL" Value="ALL" Selected="True"></asp:ListItem>
               </asp:DropDownList>
            </HeaderTemplate>

            <ItemTemplate>
                <asp:Label ID="lblname" runat="server" Text='<%# Eval("Full Name") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Mobile Number">
            <HeaderTemplate>
                Mobile Number:
               <asp:TextBox ID="txtMobilenumber" runat="server" AutoPostBack="true"></asp:TextBox>

            </HeaderTemplate>
            <ItemTemplate>
                <asp:Label ID="lblmobile" runat="server" Text='<%# Eval("Mobile Phone") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Email Address">
            <HeaderTemplate>
                Email Id:
                          <asp:TextBox ID="txtemail" runat="server" AutoPostBack="true"></asp:TextBox>
            </HeaderTemplate>
            <ItemTemplate>
                <asp:Label ID="lblemail" runat="server" Text='<%# Eval("Email ID") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>

        <asp:TemplateField HeaderText="Longitude">
            <HeaderTemplate>
                <asp:Label ID="lbllng" runat="server" Text="Longitude"></asp:Label>

            </HeaderTemplate>
            <ItemTemplate>
                <asp:Label ID="lbllongi" runat="server" Text='<%# Eval("Longitude") %>' Width="70"></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Lattitude">
            <HeaderTemplate>
                <asp:Label ID="lbllat" runat="server" Text="Lattitude"></asp:Label>

            </HeaderTemplate>
            <ItemTemplate>
                <asp:Label ID="lbllatti" runat="server" Text='<%# Eval("Lattitude") %>' Width="70"></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Activated On">
            <HeaderTemplate>
                <asp:Label ID="lbldevices" runat="server" Text="Activated On"></asp:Label></br>

            </HeaderTemplate>
            <ItemTemplate>
                <asp:Label ID="lbldevice" runat="server" Text='<%# Eval("Activated On") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Location DateTime" SortExpression="Location DateTime">
            <HeaderTemplate>
                <asp:Label ID="Label1" runat="server" Text="Location DateTime"></asp:Label></br>
                   <asp:Label ID="lblStartDate" runat="server" AssociatedControlID="txtStartDate" Text="Start Date"></asp:Label>
                <asp:TextBox ID="txtStartDate" runat="server"></asp:TextBox>
                <cc1:calendarextender id="CalendarStartDate" runat="server" format="dd/MM/yyyy" targetcontrolid="txtStartDate" popupbuttonid="imgPopup1"></cc1:calendarextender>

                <asp:ImageButton ID="imgPopup1" runat="server" ImageUrl="https://cdn4.iconfinder.com/data/icons/small-n-flat/24/calendar-alt-512.png" Height="20px" Width="30px" />
                </br>
                <asp:Label ID="lblEndDate" runat="server" Text="End Date" AssociatedControlID="txtEndDate"></asp:Label>
                &nbsp;&nbsp;<asp:TextBox ID="txtEndDate" runat="server"></asp:TextBox>
                <cc1:calendarextender id="CalendarEndDate" runat="server" format="dd/MM/yyyy" targetcontrolid="txtEndDate" popupbuttonid="imgPopup2"></cc1:calendarextender>

                <asp:ImageButton ID="imgPopup2" runat="server" ImageUrl="https://cdn4.iconfinder.com/data/icons/small-n-flat/24/calendar-alt-512.png" Height="20px" Width="30px" />

            </HeaderTemplate>
            <ItemTemplate>
                <asp:Label ID="lblDatetime" runat="server" Text='<%# Eval("Location DateTime") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

enter image description here

调试此代码时,我得到的是空值。我认为我的代码是正确的,但是我不了解文本框的价值,我不明白。请帮助我解决我的问题。

谢谢。

1 个答案:

答案 0 :(得分:4)

您可能没有将Gridview的DataBind()包装在IspostBack检查中。如果不这样做,那么值将由DataBind保留,而不是与ViewState保存。

protected void Page_Load(object sender, EventArgs e)
{
    //not here
    TrackerGrid.DataSource = source;
    TrackerGrid.DataBind();

    if (IsPostBack == false)
    {
        //but inside an ispostback check
        TrackerGrid.DataSource = source;
        TrackerGrid.DataBind();
    }
}