如何从Gridview获取文本框和下拉列表中的值并保存按钮单击事件 [英] How Can I Get The Values From Textboxes And Dropdownlist From Gridview And Save On Button Click Events

查看:71
本文介绍了如何从Gridview获取文本框和下拉列表中的值并保存按钮单击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的Gridview代码,我手动添加了文本框和下拉列表,

,在页脚中,我还添加了一个图像按钮,用于将记录保存到数据库中。

但是我不明白如何从文本框和下拉列表中获取值。





 <   asp:GridView     ID   =  GridView1    runat   =  server    AutoGenerateColumns   =  False    DataKeyNames   =  id  

OnPageIndexChanging = GridView1_PageIndexChanging OnRowCancelingEdit = GridView1_RowCancelingEdit

< span class =code-attribute> OnRowCommand = GridView1_RowCommand PageSize = 10 OnRowDeleting = GridView1_RowDeleting < span class =code-attribute>

< span class =code-attribute> < span class =code-attribute> OnRowEditing = GridView1_RowEditing class = table table-striped table-bordered datatabl es

< span class =code-attribute> AllowPaging = True OnRowDataBound = GridView1_RowDataBound >
< < span class =code-leadattribute>列 >
< asp:TemplateField HeaderText < span class =code-keyword> = 员工姓名 >
< ItemTemplate >
< asp:标签 ID = lblempname runat = server 文字 =' <% #Eval( empnam e)%>' / >
< / ItemTemplate >
< / asp:TemplateField >
< asp:TemplateField HeaderText = 日期 >
< ItemTemplate >
< asp:TextBox ID = txtdate runat = 服务器 class = 表单控制 > < / asp:TextBox >
< asp:CalendarExtender ID = txtdate_CalendarExtender runat = server 已启用 = True

< span class =code-attribute> < span class =code-attribute> TargetControlID = txtdate 格式 = dd / MM / yyyy >
< / asp:CalendarExtender >
< / ItemTemplate >
< / asp:TemplateField >
< span class =code-keyword>< asp:TemplateField HeaderText = 开始时间 >
< ItemTemplate >
< asp:TextBox ID = txtstime class = timepicker form-control runat = server > < / asp:TextBox >
< / ItemTemplate >
< / asp:TemplateField >
< asp:TemplateField HeaderText = 结束时间 >
< ItemTemplate >
< asp:TextBox ID = txtetime class = timepicker form-control runat = server > < / asp:TextBox >
< / ItemTemplate >
< < span class =code-leadattribute> / asp:TemplateField
>
< asp:TemplateField HeaderText < span class =code-keyword> = 出勤符号 >
< ItemTemplate >
< asp:DropDownList ID = ddlattsysmbol runat = server class = form-control DataSourceID = SqlDataSource1

DataTextField = 短名称 DataValueField = id AppendDataBoundItems = true >
< asp:ListItem = > 选择任何一个< / asp:ListItem >
< / asp:DropDownList >
< asp:SqlDataSource ID = SqlDataSource1 runat = 服务器 ConnectionString = <% $ ConnectionStrings: HRMConnectionString%>

< span class =code-attribute>
< span class =code-attribute> < span class =code-attribute> SelectCommand = SELECT DISTINCT * FROM [sysmbol_attend] WHERE([cid] = @cid) >
< SelectParameters >
< asp:SessionParameter < span class =code-attribute>名称 = cid SessionField = CompanyID 类型 < span class =code-keyword> = Int32 / >
< / SelectParameters >
< / asp:SqlDataSource >
< / ItemTemplate >
< / asp:TemplateField >
< asp:TemplateField >
< ItemTemplate >
< asp:ImageButton ID = imgbtnAdd runat = server ImageUrl = 〜/ images / AddNewitem .jpg

< span class =code-attribute> CommandName = AddNew 宽度 = 30px 高度 = 30px 工具提示 = 添加新用户 < span class =code-attribute> ValidationGroup = validaiton / >
< / Ite mTemplate >
< / asp:TemplateField >
< / Columns >
< PagerSettings 模式 = NextPreviousFirstLast PageButtonCount = 4 PreviousPageText = 上一个

NextPageText = 下一步 FirstPageText = 首先 LastPageText = 最后 < span class =code-keyword> / >
< / asp:GridView >







先谢谢。

解决方案

< blockquote> ConnectionStrings:HRMConnectionString%>

< span class =code-attribute> SelectCommand = < span class =code-keyword> SELECT DISTINCT * FROM [sysmbol_attend] WHERE([cid] = @cid) >
< SelectParameters >
< asp:SessionParameter 名称 = cid SessionField= \"CompanyID\" Type=\"Int32\" />
</SelectParameters>
</asp:SqlDataSource>
</ItemTemplate>
</asp:TemplateField< span class=\"code-keyword\">>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID=\"imgbtnAdd\" runat=\"server\" ImageUrl=\"~/images/AddNewit em.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.


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 :



 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.


Follow the steps:

1. Add the footer templates to gridview(i.e. Textbox, Dropdown and ImageButton).

<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\".

<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.

protected void GridView1_OnRowCommand1(object sender, GridViewCommandEventArgs e) 
{
//.....
}



4. Check for the CommandName property.

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.

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


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: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.

解决方案

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.


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 :

 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.


Follow the steps:
1. Add the footer templates to gridview(i.e. Textbox, Dropdown and ImageButton).

<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".

<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.

protected void GridView1_OnRowCommand1(object sender, GridViewCommandEventArgs e)
{
    //.....
}


4. Check for the CommandName property.

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.

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


这篇关于如何从Gridview获取文本框和下拉列表中的值并保存按钮单击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆