Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have problem when selecting item in the DataList.Website shocked after I select the item.
My connectstring:
HTML
<connectionStrings>
		<add name="sPhone" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=PhoneMgr;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>

My connect class:
C#
SqlConnection scon = new SqlConnection();
    SqlCommand cmd = new SqlCommand();
    SqlDataReader sdr;
    public void OpenConnect()
    {
        string con =    WebConfigurationManager.ConnectionStrings["sPhone"].ToString();
        if (scon.State == ConnectionState.Closed)
        {
            scon.ConnectionString = con;
            scon.Open();
        }
    }
    public DataTable GetTable(string sql, params SqlParameter[] pars)
    {
        DataTable tb = new DataTable();
        try
        {
            cmd.Connection = scon;
            cmd.CommandText = sql;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Clear();
            cmd.Parameters.AddRange(pars);
            OpenConnect();
            sdr = cmd.ExecuteReader();
            tb.Load(sdr);
            sdr.Close();
            scon.Close();
        }
        catch
        {
        }
        return tb;
    }

I have Product.aspx.It has DataList1 and lblSelected.
DataList1:
lblidProduct Text='<%#Eval("idProduct") %>'
Image:
ASP.NET
<asp:Image ID="img" runat="server" height="160px" 
            ImageUrl='<%# "~/Image/" + Eval("Image") %>' width="160px" /><br/>

ASP.NET
<a href='ProductDetail.aspx?id=<%#Eval("idProduct") %>'>Detail</a> 
<pre lang="c#"><asp:LinkButton ID="addCart" runat="server" 
                         CommandName="Select" Text="Buy" ></asp:LinkButton>

Product.aspx:
C#
Product = new Product();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            // DataList1.DataSource = dt;
            ShowProduct();
        }
    }
protected void DataList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        Label lbg = (Label)DataList1.Items[DataList1.SelectedIndex].FindControl("lblidProduct");
        lblSelected.Text=lbg.Text;
    }
Posted
Comments
OriginalGriff 10-Jun-12 1:53am    
"Shocked"? What do you mean? I have never heard of a website being mildly alarmed, let alone shocked!
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind.
Use the "Improve question" widget to edit your question and provide better information.
Stephen Hewison 10-Jun-12 4:19am    
lol
giatuan2011 10-Jun-12 7:15am    
DataList have 5 columns.the page was bouncing up when I select item in the second row.It's like being refreshed.I think so
Sandeep Mewara 10-Jun-12 2:03am    
Explain "shocked website" part.
krumia 10-Jun-12 4:35am    
By "shocked" do you mean "crashed"?

1 solution

I can see the 'DataList1_SelectedIndexChanged' event in your code. So the page refresh is inevitable since the server side event is fired. To refresh only the particular part of the page, instead of refreshing the complete one, I recommend you to use the UpdatePanel

http://ajax.net-tutorials.com/controls/updatepanel-control/[^]


http://msdn.microsoft.com/en-us/library/bb399001.aspx[^]
 
Share this answer
 

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