我有一个检查数据库的函数,以查看当前用户是否是主要用户(此值在数据库中被称为" Type"并且位值为1或0 - 主要用户用户的类型为1),如果地址列表中的地址是帐单地址(由数据库中的" IsBilling"值表示,该值也是0或1的位值。如果地址是帐单邮寄地址,然后IsBilling值为1.如果地址是送货地址,则值为0)。
我要做的是仅向主要用户显示编辑地址按钮 - 否则,应隐藏它。不过,我不想隐藏送货地址的编辑按钮。
现在,我似乎无法定位帐单邮寄地址 - 每当我将bool变量ShowButton设置为true或false时,最终都会应用于所有编辑按钮。
我检查了数据库中的SELECT查询,它确实根据当前用户ID返回所有正确的值。但我不知道为什么我无法选择何时显示/隐藏结算地址的按钮。
这是功能:
protected bool ShowButton(int UserId)
{
bool ShowButton = false;
try
{
using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["AbleCommerce"].ToString()))
{
cn.Open();
SqlCommand cmd = new SqlCommand("SELECT Type, IsBilling FROM CustomerUsers INNER JOIN Addresses ON uidUser = Addresses.UserId WHERE uidUser = @UserId", cn);
cmd.Parameters.Add(new SqlParameter("@UserId", UserId));
using (IDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
if (Convert.ToInt16(reader["Type"]) == 0 && Convert.ToInt16(reader["IsBilling"]) == 1)
{
ShowButton = false;
}
}
}
cn.Close();
}
}
catch (Exception ex)
{
Response.Write("ERROR: " + ex.Message.ToString() + "<br />");
}
return ShowButton;
}
这是带有EditAddressButton:
的转发器<asp:Repeater ID="ShipToAddressList" runat="server" OnItemCommand="ShipToAddressList_ItemCommand" >
<ItemTemplate>
<div class="entry aEntry" >
<div class="caption">
<h2><asp:Literal ID="AddressCaption" runat="server" Text='<%#((bool)Eval("IsBilling"))?"Billing Address":"Shipping Address" %>'></asp:Literal></h2>
<span class="links">
<asp:LinkButton ID="EditAddressButton" runat="server" CommandArgument='<%#Eval("AddressId")%>' Visible = '<%# ShowButton(Convert.ToInt32(Eval("UserId"))) %>' CommandName="Edit" CssClass="link"><asp:Image ID="Image1" ImageUrl="/app_themes/cartbuttons/btn_Edit.jpg" runat="server" /></asp:LinkButton><%-- --%>
<br />
<asp:LinkButton ID="DeleteAddressButton" runat="server" CommandArgument='<%#Eval("AddressId")%>' CommandName="Del" Visible='<%#!((bool)Eval("IsBilling")) %>' OnClientClick="return confirm('Are you sure to delete this address?');" CssClass="link" ><asp:Image ID="Image2" ImageUrl="/app_themes/cartbuttons/btn_Delete.jpg" runat="server" /></asp:LinkButton>
</span>
</div>
<div class="address">
<asp:Literal ID="Address" runat="server" Text='<%#Container.DataItem.ToString()%>'></asp:Literal>
<br /><asp:Literal ID="phoneAndEmail" runat="server" Text='<%# getPhoneAndEmail(AlwaysConvert.ToInt(Eval("AddressId"))) %>'></asp:Literal>
</div>
<div class="buttons">
<br /><br />
<asp:LinkButton ID="PickAddressButton" runat="server" SkinID="Button" CommandName="Pick" CommandArgument='<%#Eval("AddressId")%>' CssClass="button"><asp:Image ID="ImagePick" runat="server" ImageUrl="/app_themes/cartbuttons/btn_Ship.jpg" /></asp:LinkButton>
</div>
</div>
</ItemTemplate>
</asp:Repeater>