retrieving data in datalist from code behindhttp://forums.asp.net/t/1799195.aspx/1?retrieving+data+in+datalist+from+code+behindWed, 02 May 2012 16:09:34 -040017991954961559http://forums.asp.net/p/1799195/4961559.aspx/1?retrieving+data+in+datalist+from+code+behindretrieving data in datalist from code behind <p>Hi</p> <p>I am using datalist control and i want to change data of datalist control on click event .i am using session for giving the condition of retrieveing data from sql server in datalist but i want if session change it's value then data of datalist should&nbsp; change it's value according to the condition&nbsp; .what should i do?</p> <p>please help me</p> 2012-05-02T10:45:37-04:004961569http://forums.asp.net/p/1799195/4961569.aspx/1?Re+retrieving+data+in+datalist+from+code+behindRe: retrieving data in datalist from code behind <p>hi</p> <p>it will show you how to work and bind the data to datalist in asp.net</p> <p><a href="http://csharpektroncmssql.blogspot.com/2012/03/datalist-control-in-aspnet.html" rel="nofollow" target="_blank">http://csharpektroncmssql.<wbr>blogspot.com/2012/03/datalist-<wbr>control-in-aspnet.html</a></p> 2012-05-02T10:48:02-04:004961617http://forums.asp.net/p/1799195/4961617.aspx/1?Re+retrieving+data+in+datalist+from+code+behindRe: retrieving data in datalist from code behind <h3 class="post-title">&nbsp;bhaskar.mule ,</h3> <pre class="prettyprint">&lt;asp:DataList ID=&quot;DataList2&quot; runat=&quot;server&quot; Width=&quot;778px&quot; CellPadding=&quot;4&quot; ForeColor=&quot;#333333&quot;&gt; &lt;AlternatingItemTemplate&gt; &amp;nbsp;&lt;br /&gt; &lt;/AlternatingItemTemplate&gt; &lt;ItemTemplate&gt; &amp;nbsp;&lt;asp:Label ID=&quot;Label4&quot; runat=&quot;server&quot; Text='&lt;%# Eval(&quot;sname&quot;) %&gt;'&gt;&lt;/asp:Label&gt;-&amp;gt; &lt;asp:Label ID=&quot;Label3&quot; runat=&quot;server&quot; Text='&lt;%# Eval(&quot;message&quot;) %&gt;' &gt;&lt;/asp:Label&gt; &lt;/ItemTemplate&gt; &lt;FooterStyle BackColor=&quot;#1C5E55&quot; Font-Bold=&quot;True&quot; ForeColor=&quot;White&quot; /&gt; &lt;AlternatingItemStyle BackColor=&quot;White&quot; /&gt; &lt;ItemStyle BackColor=&quot;#E3EAEB&quot; /&gt; &lt;SelectedItemStyle BackColor=&quot;#C5BBAF&quot; Font-Bold=&quot;True&quot; ForeColor=&quot;#333333&quot; /&gt; &lt;HeaderStyle BackColor=&quot;#1C5E55&quot; Font-Bold=&quot;True&quot; ForeColor=&quot;White&quot; /&gt; &lt;/asp:DataList&gt;</pre> <pre class="prettyprint"> con.Open(); Session["frind1"] = ((LinkButton)DataList1.Items[DataList1.SelectedIndex].FindControl("LinkButton1")).Text; com = new SqlCommand(" select message,sname from msg where uid=@uid and fid=@fid", con); com.Parameters.AddWithValue("@uid",Session["uid"]); com.Parameters.AddWithValue("@fid", Session["frind1"]); dr = com.ExecuteReader(); DataList2.DataSource = dr; DataList2.DataBind(); dr.Close();<br />this is my code:-</pre> <h3 class="post-title">in this i want to bind labels in code behind<br> <br> </h3> 2012-05-02T11:02:09-04:004961625http://forums.asp.net/p/1799195/4961625.aspx/1?Re+retrieving+data+in+datalist+from+code+behindRe: retrieving data in datalist from code behind <p>Hi</p> <p>Where you change the value of session you can set again your value of session in datasource of datalist.</p> <p>Sample:</p> <p>Session[&quot;MySession&quot;] = dataTable;<br> dataList.DataSource = &nbsp;Session[&quot;MySession&quot;];<br> dataList.DataBind();</p> <p>I hope this help.&nbsp;</p> <p></p> 2012-05-02T11:05:10-04:004961716http://forums.asp.net/p/1799195/4961716.aspx/1?Re+retrieving+data+in+datalist+from+code+behindRe: retrieving data in datalist from code behind <p>I am using two datalist frist retrieve the list of friend when user select any friend and coversation between current user and his selected friend will start.i am retrieveing data on the basis of of user id and friend id if user id and friend id match to the present user and his selected friend retrieve all data of matched condition when user change or friend change then where condition should change</p> <p>of retrieveing data in datalist .meanse when user change his friend then messeage of conversation should be change in datalist control</p> <p>this is my problem cna you give me any idea</p> 2012-05-02T11:32:56-04:004961805http://forums.asp.net/p/1799195/4961805.aspx/1?Re+retrieving+data+in+datalist+from+code+behindRe: retrieving data in datalist from code behind <p>you can post your code?</p> <p>Does is need to use two datalist? Doesn't is better use two dropdown with events to SelectedIndexChange?</p> 2012-05-02T12:13:10-04:004962039http://forums.asp.net/p/1799195/4962039.aspx/1?Re+retrieving+data+in+datalist+from+code+behindRe: retrieving data in datalist from code behind <pre class="prettyprint">&lt;%@ Page Language=&quot;C#&quot; MasterPageFile=&quot;~/MasterPage.master&quot; AutoEventWireup=&quot;true&quot; CodeFile=&quot;messege.aspx.cs&quot; Inherits=&quot;Default3&quot; Title=&quot;Untitled Page&quot; %&gt; &lt;asp:Content ID=&quot;Content1&quot; ContentPlaceHolderID=&quot;ContentPlaceHolder1&quot; Runat=&quot;Server&quot;&gt; &lt;table style=&quot;width: 600px; height: 600px; background-color: #ccffff;&quot;&gt; &lt;tr&gt; &lt;td style=&quot;width: 100px; height: 596px;&quot;&gt; &lt;asp:MultiView ID=&quot;MultiView1&quot; runat=&quot;server&quot;&gt; &lt;asp:View ID=&quot;View1&quot; runat=&quot;server&quot;&gt; &lt;asp:DataList ID=&quot;DataList1&quot; runat=&quot;server&quot; DataSourceID=&quot;SqlDataSource1&quot; OnSelectedIndexChanged=&quot;DataList1_SelectedIndexChanged&quot; BackColor=&quot;White&quot; BorderColor=&quot;#CC9966&quot; BorderStyle=&quot;None&quot; BorderWidth=&quot;1px&quot; CellPadding=&quot;4&quot; GridLines=&quot;Both&quot; Width=&quot;782px&quot; SelectedIndex=&quot;0&quot;&gt; &lt;ItemTemplate&gt; &lt;asp:LinkButton ID=&quot;LinkButton1&quot; runat=&quot;server&quot; OnClick=&quot;LinkButton1_Click&quot; CommandName=&quot;select&quot; Text='&lt;%# Eval(&quot;friendname&quot;) %&gt;'&gt;&lt;/asp:LinkButton&gt; &lt;/ItemTemplate&gt; &lt;FooterStyle BackColor=&quot;#FFFFCC&quot; ForeColor=&quot;#330099&quot; /&gt; &lt;ItemStyle BackColor=&quot;White&quot; ForeColor=&quot;#330099&quot; /&gt; &lt;SelectedItemStyle BackColor=&quot;#FFCC66&quot; Font-Bold=&quot;True&quot; ForeColor=&quot;#663399&quot; /&gt; &lt;HeaderStyle BackColor=&quot;#990000&quot; Font-Bold=&quot;True&quot; ForeColor=&quot;#FFFFCC&quot; /&gt; &lt;/asp:DataList&gt;&lt;asp:SqlDataSource ID=&quot;SqlDataSource1&quot; runat=&quot;server&quot; ConnectionString=&quot;&lt;%$ ConnectionStrings:PictureAlbumConnectionString %&gt;&quot; SelectCommand=&quot;SELECT [friendname] FROM [friend] WHERE ([uid] = @uid)&quot;&gt; &lt;SelectParameters&gt; &lt;asp:SessionParameter Name=&quot;uid&quot; SessionField=&quot;uid&quot; Type=&quot;String&quot; /&gt; &lt;/SelectParameters&gt; &lt;/asp:SqlDataSource&gt; &lt;/asp:View&gt; &lt;asp:View ID=&quot;View2&quot; runat=&quot;server&quot;&gt; &lt;asp:DataList ID=&quot;DataList2&quot; runat=&quot;server&quot; Width=&quot;778px&quot; CellPadding=&quot;4&quot; ForeColor=&quot;#333333&quot;&gt; &lt;AlternatingItemTemplate&gt; &amp;nbsp;&lt;br /&gt; &lt;/AlternatingItemTemplate&gt; &lt;ItemTemplate&gt; &amp;nbsp;&lt;asp:Label ID=&quot;Label4&quot; runat=&quot;server&quot; Text='&lt;%# Eval(&quot;sname&quot;) %&gt;'&gt;&lt;/asp:Label&gt;-&amp;gt; &lt;asp:Label ID=&quot;Label3&quot; runat=&quot;server&quot; Text='&lt;%# Eval(&quot;message&quot;) %&gt;' &gt;&lt;/asp:Label&gt; &lt;/ItemTemplate&gt; &lt;FooterStyle BackColor=&quot;#1C5E55&quot; Font-Bold=&quot;True&quot; ForeColor=&quot;White&quot; /&gt; &lt;AlternatingItemStyle BackColor=&quot;White&quot; /&gt; &lt;ItemStyle BackColor=&quot;#E3EAEB&quot; /&gt; &lt;SelectedItemStyle BackColor=&quot;#C5BBAF&quot; Font-Bold=&quot;True&quot; ForeColor=&quot;#333333&quot; /&gt; &lt;HeaderStyle BackColor=&quot;#1C5E55&quot; Font-Bold=&quot;True&quot; ForeColor=&quot;White&quot; /&gt; &lt;/asp:DataList&gt;&lt;asp:SqlDataSource ID=&quot;SqlDataSource2&quot; runat=&quot;server&quot; ConnectionString=&quot;&lt;%$ ConnectionStrings:PictureAlbumConnectionString %&gt;&quot; SelectCommand=&quot;SELECT [message], [sname] FROM [msg] WHERE (([uid] = @uid) AND ([fid] = @fid))&quot;&gt; &lt;SelectParameters&gt; &lt;asp:SessionParameter Name=&quot;uid&quot; SessionField=&quot;uid&quot; Type=&quot;Int32&quot; /&gt; &lt;asp:SessionParameter Name=&quot;fid&quot; SessionField=&quot;frnd2&quot; Type=&quot;String&quot; /&gt; &lt;/SelectParameters&gt; &lt;/asp:SqlDataSource&gt; &lt;asp:TextBox ID=&quot;TextBox1&quot; runat=&quot;server&quot; Height=&quot;32px&quot; Width=&quot;267px&quot;&gt;&lt;/asp:TextBox&gt;&lt;br /&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;asp:Button ID=&quot;Button1&quot; runat=&quot;server&quot; OnClick=&quot;Button1_Click&quot; Text=&quot;Send&quot; /&gt;&lt;/asp:View&gt; &lt;/asp:MultiView&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/asp:Content&gt;</pre> <pre class="prettyprint">using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.SqlClient; public partial class Default3 : System.Web.UI.Page { SqlConnection con = new SqlConnection("Data Source=ORDIN-PC;Initial Catalog=PictureAlbum;Integrated Security=True"); SqlCommand com; SqlDataReader dr; protected void Page_Load(object sender, EventArgs e) { MultiView1.ActiveViewIndex = 0; DataList2.DataBind(); } protected void DataList1_SelectedIndexChanged(object sender, EventArgs e) { Session["frind1"] = ((LinkButton)DataList1.Items[DataList1.SelectedIndex].FindControl("LinkButton1")).Text; } protected void LinkButton1_Click(object sender, EventArgs e) { con.Open(); Session["frind1"] = ((LinkButton)DataList1.Items[DataList1.SelectedIndex].FindControl("LinkButton1")).Text; com = new SqlCommand(" select message,sname from msg where uid=@uid and fid=@fid", con); com.Parameters.AddWithValue("@uid",Session["uid"]); com.Parameters.AddWithValue("@fid", Session["frind1"]); dr = com.ExecuteReader(); DataList2.DataSource = dr; DataList2.DataBind(); dr.Close(); string abc = ("select uid from login where username=@user"); com = new SqlCommand(abc, con); com.Parameters.AddWithValue("@user", Session["frind1"].ToString()); Session["frnd2"] = (com.ExecuteScalar()); MultiView1.ActiveViewIndex = 1; } protected void Button1_Click(object sender, EventArgs e) { con.Open(); string abc = ("select name from login where uid=@uid"); com = new SqlCommand(abc, con); com.Parameters.AddWithValue("@uid", Session["uid"]); Session["tripu"] = (com.ExecuteScalar()); string p = ("insert into msg(uid,fid,message,sname)values(@uid,@fid,@message,@sname)"); com = new SqlCommand(p, con); com.Parameters.AddWithValue("@message", TextBox1.Text); com.Parameters.AddWithValue("@uid", Session["uid"]); com.Parameters.AddWithValue("@fid", Session["frnd2"]); com.Parameters.AddWithValue("@sname",Session["tripu"]); com.ExecuteNonQuery(); string q = ("insert into msg(fid,uid,message,sname)values(@uid,@fid,@message,@sname)"); com = new SqlCommand(q, con); com.Parameters.AddWithValue("@message", TextBox1.Text); com.Parameters.AddWithValue("@uid", Session["uid"]); com.Parameters.AddWithValue("@fid", Session["frnd2"]); com.Parameters.AddWithValue("@sname", Session["tripu"]); com.ExecuteNonQuery(); MultiView1.ActiveViewIndex = 1; DataList2.DataBind(); } } </pre> <p><br> <br> </p> <p></p> 2012-05-02T13:46:41-04:004962319http://forums.asp.net/p/1799195/4962319.aspx/1?Re+retrieving+data+in+datalist+from+code+behindRe: retrieving data in datalist from code behind <p>Hi</p> <p>I did any changes:</p> <p>ASPX:</p> <p></p> <blockquote><span class="icon-blockquote"></span> <h4>tripu</h4> <span class="pln">&lt;asp:LinkButton ID=&quot;LinkButton1&quot; runat=&quot;server&quot; OnClick=&quot;LinkButton1_Click&quot; CommandName=&quot;select&quot; Text='</span><span class="pun">&lt;%</span><span class="com"># Eval(&quot;friendname&quot;) </span><span class="pln">%&gt;'&gt;</span><span class="tag">&lt;/asp:LinkButton&gt;</span></blockquote> <p></p> <pre class="prettyprint">&lt;asp:LinkButton ID=&quot;LinkButton1&quot; runat=&quot;server&quot; OnClick=&quot;LinkButton1_Click&quot; CommandName=&quot;select&quot; Text='&lt;%# Eval(&quot;friendname&quot;) %&gt;' CommandArgument='&lt;%# Eval(&quot;fid&quot;) %&gt;'&gt;&lt;/asp:LinkButton&gt;</pre> <p>In CS</p> <p><blockquote><span class="icon-blockquote"></span><h4>tripu</h4><span class="typ">Session</span><span class="pun">[</span><span class="str">"frind1"</span><span class="pun">]</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> </span><span class="pun">((</span><span class="typ">LinkButton</span><span class="pun">)</span><span class="typ">DataList1</span><span class="pun">.</span><span class="typ">Items</span><span class="pun">[</span><span class="typ">DataList1</span><span class="pun">.</span><span class="typ">SelectedIndex</span><span class="pun">].</span><span class="typ">FindControl</span><span class="pun">(</span><span class="str">"LinkButton1"</span><span class="pun">)).</span><span class="typ">Text</span><span class="pun">;</span></blockquote></p> <pre class="prettyprint">Session["frind1"] = Convert.ToInt16(((LinkButton)DataList1.Items[DataList1.SelectedIndex].FindControl("LinkButton1")).CommandArgument);</pre> <p></p> <p>Observations:</p> <ul> <li>This event&nbsp;DataList1_SelectedIndexChanged is only fired when&nbsp;MultiView1.ActiveViewIndex&nbsp;=&nbsp;1 in the method&nbsp;LinkButton1_Click </li><li>I needed to change this value&nbsp;Session[&quot;frind1&quot;] in&nbsp;LinkButton1_Click because the value in LinkButton.Text is string, I think that should is integer, occur an error of type because this value; </li></ul> &lt;div&gt;&lt;/div&gt; <p><br> <br> </p> <p></p> 2012-05-02T16:09:34-04:00