[RESOLVED] How i can get the content specific label in DataList under command button?
Hello, I have application like wall facebook the user can add something on wall and the other user can comment on it.
I have done this by using DataList please review below print screen for my page translated to english.
my problem i want when user click on the button in DataList save this data in database.
Topic ID , Topic Subject.
How i can separation between Topic ID label in data list?
I using default code but naturally i get error the lblID.Text is not exist
Insert Code:
Code:
cmd = new SqlCommand("Insert into [tbWallComments] (PostID) values (N'"+lblID.Text+"')", cnn);
cnn.Open();
cmd.ExecuteScalar();
cnn.Close();
Print Screen:
http://img143.imageshack.us/img143/8748/15529799.th.png
Uploaded with ImageShack.us
DataListCode:
Code:
<asp:DataList ID="DataList1" runat="server" DataKeyField="ID"
DataSourceID="DSStudentWall"
onselectedindexchanged="DataList1_SelectedIndexChanged">
<ItemTemplate>
<table border="1" width="800px" dir="rtl" align="right">
<tr>
<td align="center" rowspan="4">
<asp:Image ID="Image2" runat="server" ImageUrl="Images/defaultprofile.gif" />
</td>
<td align="right">
<asp:Label ID="PublishNameLabel" runat="server" Font-Bold="True"
Text="publisher:" Visible="False" />
<%# Eval("PublishName") %>
</td>
</tr>
<tr>
<td align="right">
<asp:Label ID="SubjectLabel" runat="server" Font-Bold="True" Text="Subject:"
Visible="False" />
<%# Eval("Subject") %>
</td>
</tr>
<tr>
<td align="right">
<asp:Label ID="BodyLabel" runat="server" Font-Bold="True" Text="Topic:"
Visible="False" />
<%# Eval("Body") %>
</td>
</tr>
<tr>
<td align="left" width="680">
<asp:Label ID="PublishDateLabel" runat="server" Font-Bold="true"
Text="Date:" />
<%# Eval("PublishDate") %>
<asp:Label ID="PublishTimeLabel" runat="server" Font-Bold="true" Text="Time" />
<%# Eval("PublishTime") %>
<asp:Label ID="lblID" runat="server" Font-Bold="true" Text='<%# Eval("ID") %>' />
</td>
</tr>
<tr>
<td align="left" colspan="2">
<table align="center" bgcolor="#EDEBEB" dir="ltr" width="100%">
<tr>
<td align="right">
</td>
<td align="left" class="style3">
</td>
</tr>
<tr>
<td align="right">
<asp:Button ID="BtSaveComment" runat="server" CssClass="groovybutton"
Font-Bold="True" Font-Size="Medium" onclick="BtSaveComment_Click"
Text="AddComment" Width="109px"/>
<asp:TextBox ID="txtBody" runat="server" Height="50px" MaxLength="1000"
TextMode="MultiLine" Width="288px"></asp:TextBox>
</td>
<td align="left" class="style3">
<asp:Label ID="Label8" runat="server" Font-Bold="True" Font-Size="Medium"
Text=":Comment"></asp:Label>
</td>
</tr>
</table>
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
Re: How i can get the content specific label in DataList under command button?
Hello,
The lblID is created within your DataList, so as a result, there are actually multiple lblID on the page, hence why you can't reference them directly in your code.
What I would do is use the CommandArgument and CommandName properties of the AddComment Button in order to figure out which button has been pushed. Then in the ItemCommand event of the DataList you can inspect these properties and take appropriate action:
http://msdn.microsoft.com/en-us/libr...emcommand.aspx
Gary
Re: How i can get the content specific label in DataList under command button?
Thank you on attention,
I saw the example in msdn but i can't understand how i can profit of this.
Please explain to me more on my case.
Thank you
Re: How i can get the content specific label in DataList under command button?
Hello,
What I am saying is, when you bind your DataList, use the ItemDataBound to set the CommandArgument and CommandName properties of the Button within each item of the DataList.
For instance, say the TopicID was "1234".
In the DataBound event, set the CommandName of the Button to "Add" and the CommandArgument to "1234". Then, in the ItemCommand event of the DataList, inspect the CommandName of the button that has just been pushed. If it is "Add", then use the CommandArgument value to take the necessary action by inserting information into the Database.
Does that make sense?
Gary
Re: How i can get the content specific label in DataList under command button?
Thank you, Solved
I have problem in insert statement:
Code:
cmd = new SqlCommand("Insert into [tbWallComments] ([PostID],[Date],[Time],[Body],[Email]) values (N'" + e.CommandArgument.ToString() + "' , N'"+time+"' , N'"+date+"' , N'"+txtBody1.text+"' ,N'"+email+"')", cnn);
I got an exception "the name txtbody1 does not exist" , Cause this problem the textbox "txtbody1" include datalist How i can call it?
thanks again on your effort :)
Re: How i can get the content specific label in DataList under command button?
Hello,
The first thing that I need to point out is that you should NEVER concatenate an SQL String with string variables. Doing this leaves you open to SQL Injection attacks. You should ALWAYS be using Parameters. To find out more information about why this is the case you should read the "Always use Parameters" link in my signature.
Now, as for your question...
txtbody existing within the ItemTemplate of your DataList. As a result, when the page actually renders there are actually multiple instances of txtbody on the page. This means that you can't directly reference the txtbody control. Instead, you are going to need to find the required control. Assuming that you are doing what I suggested and using the ItemCommand event, you should be able to use FindControl on the current Item to locate the textbox and add it's Text property into your query.
Gary
Re: How i can get the content specific label in DataList under command button?
Quote:
Originally Posted by
gep13
The first thing that I need to point out is that you should NEVER concatenate an SQL String with string variables. Doing this leaves you open to SQL Injection attacks. You should ALWAYS be using Parameters. To find out more information about why this is the case you should read the "Always use Parameters" link in my signature.
Thank you, Always i will use Parameters.
Quote:
Originally Posted by
gep13
Assuming that you are doing what I suggested and using the ItemCommand event, you should be able to use FindControl on the current Item to locate the textbox and add it's Text property into your query.
I do not know how I can do that, How i can a used FindControl.
Please explain more , if there example it's will be very good
Thanks in advance
Re: How i can get the content specific label in DataList under command button?
Hello,
As is normally the case, the MSDN documentation has lots of examples. A quick google turned up the following:
http://msdn.microsoft.com/en-us/libr...emcommand.aspx
Which details how the ItemCommand works, and then an additional search found the following:
http://msdn.microsoft.com/en-us/libr...emcommand.aspx
Which shows how to use the FindControl method in the context of a FormView's ItemCommand, which should be very similar to the DataList.
Let me know if you can't get it to work and I will try to create a quick example.
Gary
Re: How i can get the content specific label in DataList under command button?
Thank you on your patience,
Code:
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
TextBox txtBody1 = (TextBox)DataList1.FindControl("txtBody1");
txtBody1.Text = "Test";
}
I wrote this code of the experiment , But i got on error:
Object reference not set to an instance of an object.
txtBody1.Text is text box under datalist
Re: How i can get the content specific label in DataList under command button?
Solved This is steps,
In first under page load:
Code:
for (int ii = 0; ii < DataList1.Items.Count; ii++)
{
Button ButtonSaveComm = (Button)DataList1.Items[ii].FindControl("BtSaveComment");
ButtonSaveComm.CommandName = ii.ToString();
}
In Secound under sub:
Code:
protected void calculate(object sourse, CommandEventArgs e)
{
int i = Convert.ToInt32(e.CommandName);
TextBox txtBody1 = (TextBox)DataList1.Items[i].FindControl("txtBody1");
txtBody1.Text ="test";
}
In datalist with line create button save comment:
Code:
OnCommand="calculate"
Thank you very much gep13 , I think i benefited so much from this topic :thumb:
Best Regards,
Re: [RESOLVED] How i can get the content specific label in DataList under command but
Hello,
Glad to hear that you got it all sorted out, happy that I could lend a hand!
Gary