Click here to Skip to main content
15,880,427 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
This is my Gridview code in that i have add manually textbox and dropdown,
and in that at footer, i have also add one imagebutton it is for save records into database.
but i am not understanding for how can i get the values from textboxes and dropdownlist.


ASP.NET
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="id"
                                OnPageIndexChanging="GridView1_PageIndexChanging" OnRowCancelingEdit="GridView1_RowCancelingEdit"
                                OnRowCommand="GridView1_RowCommand" PageSize="10" OnRowDeleting="GridView1_RowDeleting"
                                OnRowEditing="GridView1_RowEditing" class="table table-striped table-bordered datatables"
                                AllowPaging="True" OnRowDataBound="GridView1_RowDataBound">
                                <Columns>
                                    <asp:TemplateField HeaderText="Employee Name">
                                        <ItemTemplate>
                                            <asp:Label ID="lblempname" runat="server" Text='<%#Eval("empname") %>' />
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Date">
                                        <ItemTemplate>
                                            <asp:TextBox ID="txtdate" runat="server" class="form-control"></asp:TextBox>
                                            <asp:CalendarExtender ID="txtdate_CalendarExtender" runat="server" Enabled="True"
                                                TargetControlID="txtdate" Format="dd/MM/yyyy">
                                            </asp:CalendarExtender>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Start Time">
                                        <ItemTemplate>
                                            <asp:TextBox ID="txtstime" class="timepicker form-control" runat="server"></asp:TextBox>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="End Time">
                                        <ItemTemplate>
                                            <asp:TextBox ID="txtetime" class="timepicker form-control" runat="server"></asp:TextBox>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Attendance Symbol">
                                        <ItemTemplate>
                                            <asp:DropDownList ID="ddlattsysmbol" runat="server" class="form-control" DataSourceID="SqlDataSource1"
                                                DataTextField="shortname" DataValueField="id" AppendDataBoundItems="true">
                                                <asp:ListItem Value="">Select Any One</asp:ListItem>
                                            </asp:DropDownList>
                                            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:HRMConnectionString %>"
                                                SelectCommand="SELECT DISTINCT * FROM [sysmbol_attend] WHERE ([cid] = @cid)">
                                                <SelectParameters>
                                                    <asp:SessionParameter Name="cid" SessionField="CompanyID" Type="Int32" />
                                                </SelectParameters>
                                            </asp:SqlDataSource>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField>
                                        <ItemTemplate>
                                            <asp:ImageButton ID="imgbtnAdd" runat="server" ImageUrl="~/images/AddNewitem.jpg"
                                                CommandName="AddNew" Width="30px" Height="30px" ToolTip="Add new User" ValidationGroup="validaiton" />
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                </Columns>
                                <PagerSettings Mode="NextPreviousFirstLast" PageButtonCount="4" PreviousPageText="Previous"
                                    NextPageText="Next" FirstPageText="First" LastPageText="Last" />
                            </asp:GridView>



Thanks in Advance.
Posted
Updated 31-Jul-14 0:16am
v2

Here I don't see your footer. May be you forgot to add footer.
But if you have controls in footer here is how you find the control and get the value :

C#
 string yourName = string.Empty;
 string yourCity = string.Empty;
//Gets the footer row directly
 GridViewRow row = GridView1.FooterRow;

 yourName = ((TextBox)row.FindControl("yourTextBoxId")).Text;//Get the value of textbox
 yourCity = ((DropDownList)row.FindControl("yourDropDownListId")).SelectedValue; //get the     selected value


Hope you got it :)
Good luck.
 
Share this answer
 
v2
Comments
_Amy 31-Jul-14 6:46am    
+5! :)
Raje_ 31-Jul-14 6:54am    
Tnx Amit :)
Follow the steps:
1. Add the footer templates to gridview(i.e. Textbox, Dropdown and ImageButton).
ASP.NET
<columns>
  <asp:templatefield headertext="Test" xmlns:asp="#unknown">
    <itemtemplate>
       .... 
    </itemtemplate>
    <footertemplate>
      <asp:textbox id="Textbox1" runat="server" />
    </footertemplate>
  </asp:templatefield>
</columns>

2. Assign the CommandName Property of ImageButton to "Add".
ASP.NET
<columns>
  <asp:templatefield headertext="Test" xmlns:asp="#unknown">
    <itemtemplate>
       .... 
    </itemtemplate>
    <footertemplate>
      <asp:imagebutton id="ImgBtn" runat="server" commandname="Add" />
    </footertemplate>
  </asp:templatefield>
</columns>

3. Handle RowCommand event of GridView.
C#
protected void GridView1_OnRowCommand1(object sender, GridViewCommandEventArgs e)
{
    //.....
}

4. Check for the CommandName property.
C#
protected void GridView1_OnRowCommand1(object sender, GridViewCommandEventArgs e)
{
    //Check for CommandName "Add"
    if (e.CommandName.Equals("Add"))
    {
        //Do your operation
    }
}

5. Find the footer control and do your operation.
C#
protected void GridView1_OnRowCommand1(object sender, GridViewCommandEventArgs e)
{
    //Check for CommandName "Add"
    if (e.CommandName.Equals("Add"))
    {
        //Find the controls
        TextBox sp = GridView1.FooterRow.FindControl("Textbox1") as TextBox;
        DropDownList dT = GridView1.FooterRow.FindControl("DropDown1") as DropDownList;
        
        //Do your operation
    }
}


See a demo here[^].

--Amy
 
Share this answer
 
Comments
Raje_ 31-Jul-14 6:44am    
+5 for great explanation :)
_Amy 31-Jul-14 6:45am    
Thank Rajesh. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900