如何在按钮单击时获取文本框文本存储在数据库中 [英] how to get textbox text to store in database on button click
问题描述
你好,我在asp.net中遇到这个小问题
i有这样的代码...
< asp:DataList ID = DataList1 runat = 服务器 >
< ItemTemplate >
< asp:标签 ID = Label2 runat = 服务器 正文 =' <% #Eval( status)%> ' > < / asp:Label >
< br / > < hr / >
< asp:DataList ID = DataList2 < span class =code-attribute> runat = server DataSource =' <% #getCommentsByStatID(Eval( statid )。ToString())%> ' >
< ItemTemp迟到 >
< asp:标签 ID = Label1 runat = server 文本 =' <% #Eval( comment)%> ' > < / asp:标签 >
< / ItemTemplate >
< / asp:DataList >
< asp:TextBox ID = txtcomment runat = server 文字 =' <% #Container.DataItem %> ' > < / asp:TextBox >
< asp:按钮 ID = btncomment runat = server 文本 = 发布 PostBackUrl =' <%# 〜/ Default2.aspx?statusid = + Eval( statid)%> ' / >
< / ItemTemplate >
< SeparatorTemplate < span class =code-keyword>> < hr / > < / SeparatorTemplate >
< / asp:DataList >
按钮btncomment是点击,我需要通过知道状态ID来获取保存在数据库中的txtcomment文本..我可以从查询字符串中获取状态ID但我无法获取txtcomment的文本..
请帮助一些合适的代码或链接.........
感谢
在你的datalist上添加ItemCommand Event,如下所示。
< asp:datalist id = DataList1 runat = server onitemcommand = DataList1_ItemCommand xmlns:asp = #未知 >
< itemtemplate >
< asp:textbox id = txtcomment runat = server text = > < / asp:textbox >
< ; / itemtemplate >
< / asp:datalist >
然后在项目命令事件中,您可以轻松获取文本框的文本。
protected void DataList1_ItemCommand( object source,DataListCommandEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
TextBox txtComments =(TextBox)e.Item.FindControl( txtcomment);
string Comments = txtComments.Text; // 此处您可以从文本框中获取文字
}
}
接受解决方案,如果它可以帮助你的
hello frens, i have this small problem in asp.net
i have a code like this...
<asp:DataList ID="DataList1" runat="server" >
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%#Eval("status") %>'></asp:Label>
<br /> <hr />
<asp:DataList ID="DataList2" runat="server" DataSource='<%#getCommentsByStatID(Eval("statid").ToString()) %>'>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#Eval("comment") %>'></asp:Label>
</ItemTemplate>
</asp:DataList>
<asp:TextBox ID="txtcomment" runat="server" Text='<%# Container.DataItem %>' ></asp:TextBox>
<asp:Button ID="btncomment" runat="server" Text="Post" PostBackUrl='<%# "~/Default2.aspx?statusid="+Eval("statid")%>' />
</ItemTemplate>
<SeparatorTemplate><hr /></SeparatorTemplate>
</asp:DataList>
when the button btncomment is clicked, i need to get the text of txtcomment saved in the database by knowing the status id.. i can get the status id from the query string but i cannot get the text of txtcomment ..
please help with some suitable codes or links .........
with thanks
Add ItemCommand Event on your datalist as given below.
<asp:datalist id="DataList1" runat="server" onitemcommand="DataList1_ItemCommand" xmlns:asp="#unknown"> <itemtemplate> <asp:textbox id="txtcomment" runat="server" text=""></asp:textbox> </itemtemplate> </asp:datalist>
Then on item command event, you can get text of your textbox easily.
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e) { if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { TextBox txtComments = (TextBox)e.Item.FindControl("txtcomment"); string Comments = txtComments.Text; // here you can get text from your textbox } }
Accept Solution if it helps you.
这篇关于如何在按钮单击时获取文本框文本存储在数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!