如果条件在Datalist Itemcommand中不起作用 [英] if condition is not working in Datalist Itemcommand
本文介绍了如果条件在Datalist Itemcommand中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一直在购物车项目中工作,如果条件不起作用并且值没有插入添加购物车表,我会被卡在添加到购物车模块中它显示空网格@code如下
ViewProductDetails.aspx.cs
受保护 void DataList1_ItemCommand( object source,DataListCommandEventArgs e)
{
if (e.CommandName == 购物车)
{
Response.Redirect( Addtocart.aspx?ProductID = + e.CommandArgument。的ToString());
da = new SqlDataAdapter( select count (*)来自rsa_addtocart tatc内部连接rsa_Users tl on tatc.UserId = tl.UserId其中tatc.productid = + Convert.ToInt32(e.CommandArgument.ToString())+ 和tl.UserId = + Convert.ToInt32(会话[ < span class =code-string> UserId]。ToString())+ 和tatc。 UserId = + Convert.ToInt32(会话[ UserId]。ToString() )+ ,con);
int n = Convert.ToInt32(da.SelectCommand.ExecuteScalar());
if (n == 1 )
{
da = new SqlDataAdapter( select tpd.ProductID,tpd.ProductName ,tpd.Price,tpd.ProductImage,tl.UserId,tatc.cartId from rsa_ProductItemTable tpd inner join rsa_addtocart tatc on tpd.ProductID = tatc.productid inner join rsa_Users tl on tl.UserId = tatc.UserId where tatc.productid = + Convert.ToInt32(e.CommandArgument.ToString())+ 和tl.UserId = + Convert.ToInt32(会话[ UserId]。ToString())+ ,con);
ds = new DataSet();
da.Fill(ds, tbl_tpd);
if (ds.Tables.Count > 0 && ds.Tables [ tbl_tpd]。Rows.Count > 0 )
{
da = new SqlDataAdapter( update rsa_addtocart set UserId = + Convert.ToInt32(会话[ UserId]。ToString())+ ,productid = + Convert.ToInt32(ds.Tables [ 0 ]。行[ 0 ] [ 0 ]。ToString())+ ,productname =' + ds.Tables [ 0 ]。行[ 0 ] [ 1 ]。ToString()+ ',ProductImage =' + ds.Tables [ 0 ]。行[ 0 ] [ 3 ]。ToString()+ ',price = + Convert.ToDecimal(ds.Tables [ 0 ]。行[ 0 ] [ 2 ]。ToString())+ ,cdate = getdate()其中productid = + Convert.ToInt32(ds.Tables [ 0 ]。行[ 0 ] [ 0 ]。ToString ())+ 和cartId = + Convert.ToInt32(ds.Tables [ 0 ]。行[ 0 ] [ 5 ]。ToString())+ 和UserId = + Convert.ToInt32(会话[ UserId]。ToString())+ ,con);
da.SelectCommand.ExecuteNonQuery();
}
}
其他
{
da = 新 SqlDataAdapter( 从rsa_ProductItemTable中选择tpd.ProductID,tpd.ProductName,tpd.Price,tpd.ProductImage tpd其中tpd.ProductID = + Convert.ToInt32(e.CommandArgument.ToString())+ ,con);
ds = new DataSet();
da.Fill(ds, tbl_tpd);
}
如果(ds.Tables.Count > 0 && ds.Tables [ tbl_tpd ] .Rows.Count > 0 )
{
da = new SqlDataAdapter( 插入rsa_addtocart(UserId,productid,productname, ProductImage,price,totalcost,cdate)值( + Convert.ToInt32(会话[ UserId]。ToString())+ , + Convert.ToInt32(ds.Tables [ 0 ]。行[ 0 ] [ 0 ]。ToString())+ ,' + ds.Tables [ 0 ]。行[ 0 ] [ 1 ]。ToString()+ ',' + ds.Tables [ 0 ]。行[ 0 ] [ 3 ]。ToString()+ ', + Convert.ToDecimal(ds.Tables [ 0 ]。行[ 0 ] [ 2 ]。ToString())+ ,1, + Convert.ToDecimal(ds.Tables [ 0 ]。行[ 0 ] [ 2 ]。ToString())+ ,getdate()),con);
da.SelectCommand.ExecuteNonQuery();
}
}
Addtocart.aspx
< asp:GridView ID = GridView1 runat = server CellPadding = 4 ForeColor = #333333 GridLines = 无 DataKeyNames = productid
Style = position:static AutoGenerateColumns = False OnRowDeleting = GridView1_RowDeleting OnPageIndexChanging = GridView1_PageIndexCha nging AllowPaging = True PageSize = 5 >
< FooterStyle BackColor = #507CD1 Font-Bold = True ForeColor = White />
< RowStyle BackColor = #EFF3FB />
< EditRowStyle BackColor = #2461BF />
< SelectedRowStyle BackColor = #D1DDF1 Font-Bold = True ForeColor = #333333 />
< PagerStyle BackColor = #2461BF ForeColor = 白色 HorizontalAlign = 中心 />
< HeaderStyle BackColor = #507CD1 Font-Bold = True ForeColor = 白色 />
< AlternatingRowStyle BackColor = White />
< Columns>
< asp:TemplateField HeaderText = CartId >
< ItemTemplate>
< asp:标签ID = cartid runat = server Text = ' <%#Eval(cartId)%>' > < / asp:标签 >
< / ItemTemplate >
< / asp:TemplateField >
< asp:TemplateField HeaderText = 图像 >
< ItemTemplate>
< asp:图片ID = imgpath runat = server Width = 100高度= 100 ImageUrl = ' <%#Eval(ProductImage)%>' />
< / ItemTemplate >
< / asp:TemplateField >
< asp:TemplateField HeaderText = ProductName >
< ItemTemplate>
< asp:Label ID = bname runat = server Text = ' <%#Eval(productname)%>' > < / asp:标签 >
< / ItemTemplate >
< / asp:TemplateField >
< asp:TemplateField HeaderText = Price >
< ItemTemplate>
< asp:标签ID = cost runat = server Text = ' <%#Eval(price)%>' > < / asp:标签 >
< / ItemTemplate >
< / asp:TemplateField >
< asp:TemplateField HeaderText = 数量 >
< ItemTemplate>
< asp:TextBox ID = qty runat = server Width = 20 Text = ' <%#Eval(qty)%>' AutoPostBack = True OnTextChanged = qty_TextChanged > < / / span> asp:TextBox >
< ; / ItemTemplate >
< / asp:TemplateField >
< asp:TemplateField HeaderText = 总计 >
< ItemTemplate>
< asp:Label ID = tcost runat = server Width = 100 Text = ' <%#Eval(totalcost)%>' > < / asp:Label >
< / ItemTemplate >
< / asp:TemplateField >
< asp:TemplateField HeaderText = 删除 >
<&的ItemTemplate GT;
< asp:LinkButton ID = delete runat = server Text = 删除 CommandName = 删除 CommandArgument = ' <%#Eval(cartId)%>' > < / asp:LinkButton >
< / ItemTemplate >
< / asp:TemplateField >
< / 列 >
< EmptyDataTemplate>
< strong>购物车中没有商品。< / strong >
< / < span class =code-leadattribute> EmptyDataTemplate >
< / asp:GridView >
Addtocart.aspx.cs
< pre lang = cs> protected void Page_Load( object sender,EventArgs e)
{
if (Page。 IsPostBack!= true )
{
GetUserSelectedproducts(Convert.ToInt32(Session [ 用户ID跨度>]的ToString()))。
GetTotal();
}
}
< pre> private void GetUserSelectedproducts( int uid)
{
da = new SqlDataAdapter( 选择tatc.cartId,tatc.productname,tatc.ProductImage,tatc.price,tatc.totalcost from rsa_addtocart tatc inner join rsa_Users tl on tl.UserId = tatc.UserId where tatc.UserId = + uid + ,con);
ds = new DataSet();
da.Fill(ds, tbl_tatc);
GridView1.DataSource = ds.Tables [ tbl_tatc]。DefaultView;
GridView1.DataBind();
}
< / pre >
请提前帮助我。
解决方案
使用
if(e.Item.ID ==ItemId)
{
//放在这里你的代码
}
和数据列表中的Comand Id = ItemId
请调试此行并查看返回的内容。可能有一些行显示在网格上或只是一个空行。调试并查看行数。
GridView1.DataSource = ds.Tables [ tbl_tatc跨度>]默认视图;
i have been working in the shopping cart project and i have been stuck in add to cart module if condition is not working and values are not inserting into the add cart table it displays empty grid @code as follows
ViewProductDetails.aspx.cs
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "Cart")
{
Response.Redirect("Addtocart.aspx?ProductID=" + e.CommandArgument.ToString());
da = new SqlDataAdapter("select count(*) from rsa_addtocart tatc inner join rsa_Users tl on tatc.UserId=tl.UserId where tatc.productid=" + Convert.ToInt32(e.CommandArgument.ToString()) + " and tl.UserId=" + Convert.ToInt32(Session["UserId"].ToString()) + " and tatc.UserId=" + Convert.ToInt32(Session["UserId"].ToString()) + " ", con);
int n = Convert.ToInt32(da.SelectCommand.ExecuteScalar());
if (n == 1)
{
da = new SqlDataAdapter("select tpd.ProductID,tpd.ProductName,tpd.Price,tpd.ProductImage,tl.UserId,tatc.cartId from rsa_ProductItemTable tpd inner join rsa_addtocart tatc on tpd.ProductID=tatc.productid inner join rsa_Users tl on tl.UserId=tatc.UserId where tatc.productid=" + Convert.ToInt32(e.CommandArgument.ToString()) + " and tl.UserId=" + Convert.ToInt32(Session["UserId"].ToString()) + " ", con);
ds = new DataSet();
da.Fill(ds, "tbl_tpd");
if (ds.Tables.Count > 0 && ds.Tables["tbl_tpd"].Rows.Count > 0)
{
da = new SqlDataAdapter("update rsa_addtocart set UserId=" + Convert.ToInt32(Session["UserId"].ToString()) + ",productid=" + Convert.ToInt32(ds.Tables[0].Rows[0][0].ToString()) + ",productname='" + ds.Tables[0].Rows[0][1].ToString() + "',ProductImage='" + ds.Tables[0].Rows[0][3].ToString() + "',price=" + Convert.ToDecimal(ds.Tables[0].Rows[0][2].ToString()) + ",cdate=getdate() where productid=" + Convert.ToInt32(ds.Tables[0].Rows[0][0].ToString()) + " and cartId=" + Convert.ToInt32(ds.Tables[0].Rows[0][5].ToString()) + " and UserId=" + Convert.ToInt32(Session["UserId"].ToString()) + " ", con);
da.SelectCommand.ExecuteNonQuery();
}
}
else
{
da = new SqlDataAdapter("select tpd.ProductID,tpd.ProductName,tpd.Price,tpd.ProductImage from rsa_ProductItemTable tpd where tpd.ProductID=" + Convert.ToInt32(e.CommandArgument.ToString()) + " ", con);
ds = new DataSet();
da.Fill(ds, "tbl_tpd");
}
if (ds.Tables.Count > 0 && ds.Tables["tbl_tpd"].Rows.Count > 0)
{
da = new SqlDataAdapter("insert into rsa_addtocart(UserId,productid,productname,ProductImage,price,totalcost,cdate)values(" + Convert.ToInt32(Session["UserId"].ToString()) + "," + Convert.ToInt32(ds.Tables[0].Rows[0][0].ToString()) + ",'" + ds.Tables[0].Rows[0][1].ToString() + "','" + ds.Tables[0].Rows[0][3].ToString() + "'," + Convert.ToDecimal(ds.Tables[0].Rows[0][2].ToString()) + ",1," + Convert.ToDecimal(ds.Tables[0].Rows[0][2].ToString()) + ",getdate())", con);
da.SelectCommand.ExecuteNonQuery();
}
}
Addtocart.aspx
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" DataKeyNames="productid"
Style="position: static" AutoGenerateColumns="False" OnRowDeleting="GridView1_RowDeleting" OnPageIndexChanging="GridView1_PageIndexChanging" AllowPaging="True" PageSize="5">
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#EFF3FB" />
<EditRowStyle BackColor="#2461BF" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="CartId">
<ItemTemplate>
<asp:Label ID="cartid" runat="server" Text='<%#Eval("cartId") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Image">
<ItemTemplate>
<asp:Image ID="imgpath" runat="server" Width="100" Height="100" ImageUrl='<%#Eval("ProductImage") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ProductName">
<ItemTemplate>
<asp:Label ID="bname" runat="server" Text='<%#Eval("productname") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Price">
<ItemTemplate>
<asp:Label ID="cost" runat="server" Text='<%#Eval("price") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Quantity">
<ItemTemplate>
<asp:TextBox ID="qty" runat="server" Width="20" Text='<%#Eval("qty") %>' AutoPostBack="True" OnTextChanged="qty_TextChanged"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Total">
<ItemTemplate>
<asp:Label ID="tcost" runat="server" Width="100" Text='<%#Eval("totalcost") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:LinkButton ID="delete" runat="server" Text="Delete" CommandName="Delete" CommandArgument='<%#Eval("cartId") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
<strong>There are no items in your cart.</strong>
</EmptyDataTemplate>
</asp:GridView>
Addtocart.aspx.cs
<pre lang="cs">protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack != true)
{
GetUserSelectedproducts(Convert.ToInt32(Session["UserId"].ToString()));
GetTotal();
}
}
<pre>private void GetUserSelectedproducts(int uid)
{
da = new SqlDataAdapter("select tatc.cartId,tatc.productname,tatc.ProductImage,tatc.price,tatc.totalcost from rsa_addtocart tatc inner join rsa_Users tl on tl.UserId=tatc.UserId where tatc.UserId=" + uid + " ", con);
ds = new DataSet();
da.Fill(ds, "tbl_tatc");
GridView1.DataSource = ds.Tables["tbl_tatc"].DefaultView;
GridView1.DataBind();
}
</pre>
please me help me with this thanks in advance.
解决方案
use
if(e.Item.ID == "ItemId")
{
//put here your Code
}
and in data list set Comand Id =ItemId
Please debug this line and see what this is returning. There might be some row, which is showing on Grid or just a blank row. Debug and see the count of Rows.
GridView1.DataSource = ds.Tables["tbl_tatc"].DefaultView;
这篇关于如果条件在Datalist Itemcommand中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文