I have problem when selecting item in the DataList.Website shocked after I select the item.
My connectstring:
<connectionStrings>
<add name="sPhone" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=PhoneMgr;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
My connect class:
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:Image ID="img" runat="server" height="160px"
ImageUrl='<%# "~/Image/" + Eval("Image") %>' width="160px" /><br/>
<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:
Product = new Product();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ShowProduct();
}
}
protected void DataList1_SelectedIndexChanged(object sender, EventArgs e)
{
Label lbg = (Label)DataList1.Items[DataList1.SelectedIndex].FindControl("lblidProduct");
lblSelected.Text=lbg.Text;
}