Change visibility of button in gridview for different users - c#

I have a gridview of comments which shows a delete button for the comment made by logged in user.I mean logged in uder can delete his own comments ONLY.
I have tried the below but it doesnt show the delete button for any user.
<asp:GridView ID="GridView1" runat="server" GridLines="None" AutoGenerateColumns="False" Width="540px" ShowHeader="False" CellPadding="4" ForeColor="Black" OnRowCommand="GridView1_RowCommand" DataKeyNames="CommentId">
<EmptyDataTemplate>
---No comments on this event---
</EmptyDataTemplate>
<Columns>
<asp:TemplateField>
<ItemTemplate>
<table style="width:540px;background-color:white">
<tr>
<td style="width:60px" rowspan="3">
<asp:Image ID="imgUser" Width="50px" Height="50px" ImageUrl='<%# Bind("ProfilePicPath") %>' runat="server" style="border: 2px inset #C0C0C0" />
</td>
<td style="width:480px;border-bottom:solid 1px #999999">
<asp:Label ID="lblCommentator" runat="server" Text='<%# Bind("FirstName") %>' style="color: #336699"></asp:Label>
on
<asp:Label ID="lblCommentTime" runat="server" Text='<%# Bind("CommentTime") %>' style="font-weight: 700"></asp:Label>
</td>
</tr>
<tr>
<td style="width:480px;vertical-align:top">
<asp:Label ID="lblComment" runat="server" Text='<%# Bind("Comment") %>'></asp:Label>
</td>
</tr>
<tr>
<td style="width:480px;vertical-align:top;text-align:right">
<asp:HiddenField ID="hfUserId" Value='<%# Bind("UserId") %>' runat="server" />
<asp:LinkButton ID="btnDeleteComment" runat="server" Visible='<%# Session["UserId"] == Eval("UserId") %>' CommandArgument='<%#Eval("CommentId")%>' CommandName="Delete">Delete</asp:LinkButton>
</td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:SportsActiveConnectionString %>" SelectCommand="spExtractEventComments" SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:ControlParameter Name="Eventid" Type="Int32" ControlID="lblEventId" />
</SelectParameters>
</asp:SqlDataSource>
As you can see above, i am comparing the current user(stored in session) to the UserId of that comment.
<asp:LinkButton ID="btnDeleteComment" runat="server" Visible='<%# Session["UserId"] == Eval("UserId") %>'.....
Thanks in advance

There is example :
vb.net :
<asp:LinkButton ID="btnDeleteComment" runat="server" Visible='<%# If(Session("UserId") = Eval("UserId"), "True", "False")%>'></asp:LinkButton>
c# (I think) :
<asp:LinkButton ID="btnDeleteComment" runat="server" Visible='<%# If(Session["UserId"] == Eval("UserId").ToString() ? true : false)%>'></asp:LinkButton>
Using If for checking are values match (Tested and working)
If(condition,do what You want if result is True, do what You want if result if False)

Related

Why data is not displayed on my asp.net webpage?

I am developing Canteen Management System where I am displaying menuList from Database using gridview like given below.. ( this is Menu.aspx page)
<asp:DataList ID="DataList1" runat="server" RepeatColumns="3" RepeatDirection="Horizontal" OnItemCommand="DataList1_ItemCommand">
<ItemTemplate>
<table class="nav-justified" style="height: 111px">
<tr>
<td>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("menuName") %>'></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:ImageButton ID="ImageButton1" runat="server" CommandName="viewDetail" CommandArgument='<%# Eval("Id") %>' ImageUrl='<%# Eval("menuImage") %>' />
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("menuPrice") %>'></asp:Label>
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
<br />
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Cms_AspFormsConnectionString %>" SelectCommand="SELECT [menuName], [menuPrice], [menuImage], [Id] FROM [menuInfo]"></asp:SqlDataSource>
This is Menu.aspx.cs page
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
if ( e.CommandName == "viewDetail" )
{
Response.Redirect("MenuDetails.aspx?Id=" + e.CommandArgument.ToString());
}
}
but When i run this this it's showing nothing, ( when i go to source page it was all empty at like given below..)
<div>
<br />
</div>
Why it' showing empty? and how to resolve this?
Add DataSourceID="SqlDataSource1" to the DataList tag. Data source was not set for the control.

Why my datalist is displaying only first image?

I have db which contains products with columns: image1, image2, image3. I'm displaying this datalist and only first picture occurs there. On the others images I see that redirection is fine, but it isn't showing them up.
Markup:
<asp:DataList ID="DataList2" runat="server" DataKeyField="ID" DataSourceID="SqlDataSource1">
<ItemTemplate>
<table>
<tr>
<td rowspan="2">
<asp:HyperLink ID="HyperLink3" runat="server" ImageUrl='<%# Eval("image1", "{0}") %>' data-lightbox="imageset" Height="150px" Width="150px" ImageHeight="150px" ImageWidth="150px" NavigateUrl='<%# Eval("image1", "{0}") %>'></asp:HyperLink>
</td>
<td width="600px"><asp:Label ID="nazwaLabel" runat="server" Text='<%# Eval("nazwa") %>' CssClass="produktNazwa" />
</tr>
<tr>
<td><asp:Label ID="cenaLabel" runat="server" Text='<%# Eval("cena", "{0} PLN") %>' CssClass="produktCena" /> </td>
</tr>
<tr>
<td>
<asp:HyperLink ID="HyperLink4" runat="server" ImageUrl='<%# Eval("image2", "{0}") %>' data-lightbox="imageset" Height="150px" Width="150px" ImageHeight="150px" ImageWidth="150px" NavigateUrl='<%# Eval("image2", "{0}") %>'></asp:HyperLink>
</td>
<td><asp:Label ID="opisLabel" runat="server" Text='<%# Eval("opis") %>' CssClass="produktOpis" /></td>
</tr>
<tr>
<td>
<asp:HyperLink ID="HyperLink5" runat="server" ImageUrl='<%# Eval("image3", "{0}") %>' data-lightbox="imageset" Height="150px" Width="150px" ImageHeight="150px" ImageWidth="150px" NavigateUrl='<%# Eval("image3", "{0}") %>'></asp:HyperLink>
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * FROM [Produkty] WHERE ([ID] = #ID)">
<SelectParameters>
<asp:ControlParameter ControlID="Label1" Name="ID" PropertyName="Text" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
Any ideas?

SqlDataSource UpdateCommand parameters not changing

I have a FormView with an UpdateButton and a SqlDataSource which are below. My updatebutton always update the Sil table with the same values (the values at the first line of my gridview), I couldn't figure out why.
<asp:FormView ID="frm_Benefit" runat="server" DataKeyNames="ID" DataSourceID="sds_Benefits"
OnItemInserted="frm_Benefit_ItemInserted" OnItemUpdated="frm_Benefit_ItemUpdated">
<EditItemTemplate>
<table class="formview">
<tr>
<td>
Code:
</td>
<td>
<asp:TextBox ID="CodeTextBox" runat="server" Text='<%# Bind("Code") %>' />
</td>
</tr>
<tr>
<td>
Name:
</td>
<td>
<asp:TextBox ID="NameTextBox" runat="server" Text='<%# Bind("Name") %>' />
</td>
</tr>
<tr>
<td>
HRName:
</td>
<td>
<asp:TextBox ID="HRNameTextBox" runat="server" Text='<%# Bind("HRName") %>' />
</td>
</tr>
<tr>
<td>
Description:
</td>
<td>
<asp:TextBox ID="DescriptionTextBox" runat="server" Text='<%# Bind("Description") %>'
TextMode="MultiLine" MaxLength="200" />
</td>
</tr>
<tr>
<td>
Associated Url:
</td>
<td>
<asp:TextBox ID="UrlTextBox" runat="server" Text='<%# Bind("Url") %>' MaxLength="100" />
</td>
</tr>
<tr>
<td>
BenefitType:
</td>
<td>
<asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="sds_BenefitTypes"
DataTextField="Name" DataValueField="ID" SelectedValue='<%# Bind("BenefitTypeID") %>'>
</asp:DropDownList>
</td>
</tr>
<tr>
<td>
Cost:
</td>
<td>
<asp:TextBox ID="CostTextBox" runat="server" Text='<%# Bind("Cost") %>' />
</td>
</tr>
<tr>
<td>
Cost Type:
</td>
<td>
<asp:DropDownList ID="CostTypeDropDown" runat="server" DataSourceID="sds_CostTypes"
DataTextField="Name" DataValueField="ID" SelectedValue='<%# Bind("CostTypeID") %>'>
</asp:DropDownList>
</td>
</tr>
<tr>
<td>
Quantity Type:
</td>
<td>
<asp:DropDownList ID="QuantityTypeDropDown" runat="server" DataSourceID="sds_QuantityTypes"
DataTextField="Name" DataValueField="ID" SelectedValue='<%# Bind("QuantityTypeID") %>'>
</asp:DropDownList>
</td>
</tr>
<tr>
<td>
Quantity Entry On Add:
</td>
<td>
<asp:CheckBox ID="QuantityEntryOnAddCheckBox" runat="server" Checked='<%# Bind("QuantityEntryOnAdd") %>' />
</td>
</tr>
<tr>
<td>
Quantity Entry On Remove:
</td>
<td>
<asp:CheckBox ID="QuantityEntryOnRemoveCheckBox" runat="server" Checked='<%# Bind("QuantityEntryOnRemove") %>' />
</td>
</tr>
<tr>
<td>
Bonus Ratio:
</td>
<td>
<asp:TextBox ID="BonusRatioTextBox" runat="server" Text='<%# Bind("BonusRatio") %>'></asp:TextBox>
</td>
</tr>
<tr>
<td>
ApplyIncomeTax:
</td>
<td>
<asp:CheckBox ID="ApplyIncomeTaxCheckBox" runat="server" Checked='<%# Bind("ApplyIncomeTax") %>' />
</td>
</tr>
<tr>
<td>
ApplyStampTax:
</td>
<td>
<asp:CheckBox ID="ApplyStampTaxCheckBox" runat="server" Checked='<%# Bind("ApplyStampTax") %>' />
</td>
</tr>
<tr>
<td class="style1">
ApplySocialSecurityTax:
</td>
<td class="style1">
<asp:CheckBox ID="ApplySocialSecurityTaxCheckBox" runat="server" Checked='<%# Bind("ApplySocialSecurityTax") %>' />
</td>
</tr>
<tr>
<td>
Applicable Location:
</td>
<td>
<asp:DropDownList ID="CostTypeDropDown1" runat="server" DataSourceID="sds_Locations"
DataTextField="Name" DataValueField="ID" SelectedValue='<%# Bind("ApplicableLocationID") %>'>
</asp:DropDownList>
</td>
</tr>
<tr>
<td colspan="2">
<asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update"
Text="Update" Style="display: none" />
<asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False"
CommandName="Cancel" Text="Cancel" Style="display: none" />
</td>
</tr>
</table>
</EditItemTemplate>
</asp:FormView>
<asp:SqlDataSource ID="sds_Benefits" runat="server" ConnectionString="<%$ ConnectionStrings:ConnFlexibleBenefitsDB %>"
SelectCommand="SELECT * FROM [View_Benefits]"
UpdateCommand="UPDATE [Sil] SET [Code] = #Code, [HRName] = #HRName WHERE [Id] = #Id">
<UpdateParameters>
<asp:Parameter Name="Code" Type="String" />
<asp:Parameter Name="HRName" Type="String" />
<asp:Parameter Name="Id" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
And this is my gridview:
<asp:GridView ID="grd_Benefits" runat="server" AutoGenerateColumns="False" DataKeyNames="ID"
CssClass="gridTable" DataSourceID="sds_Benefits" OnRowCommand="grd_Benefits_RowCommand">
<RowStyle CssClass="gridRow" />
<FooterStyle CssClass="gridFooter" />
<SelectedRowStyle CssClass="gridSelectedRow" />
<HeaderStyle CssClass="gridHeader" />
<AlternatingRowStyle CssClass="gridAlternatingRow" />
<EmptyDataTemplate>
No records found.
</EmptyDataTemplate>
<Columns>
<asp:BoundField DataField="Code" HeaderText="Code" SortExpression="Code" />
<asp:BoundField DataField="HRName" HeaderText="HRName" SortExpression="HRName" />
<asp:TemplateField HeaderText="Benefit Type" SortExpression="BenefitType.Name">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("BenefitTypeName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Cost" HeaderText="Cost" SortExpression="Cost" />
<asp:TemplateField HeaderText="Cost Type">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("CostTypeName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Quantity Type">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("QuantityTypeName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lbSelect" runat="server" CausesValidation="False" CommandName="ViewRecord"
Text="<img border='0' alt='Edit' src='../images/zoom.gif' />" CommandArgument='<%# Container.DataItemIndex %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<HeaderTemplate>
<asp:LinkButton ID="lbInsert" runat="server" CausesValidation="false" CommandName="NewRecord"
Text="<img border='0' alt='New' src='../images/new.gif' />" CommandArgument='<%# Container.DataItemIndex %>'
ForeColor="White"></asp:LinkButton>
</HeaderTemplate>
<ItemTemplate>
<asp:LinkButton ID="lbEdit" runat="server" CausesValidation="False" CommandName="EditRecord"
Text="<img border='0' alt='Edit' src='../images/edit.gif' />" CommandArgument='<%# Container.DataItemIndex %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
RowCommand:
protected void grd_Benefits_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "ViewRecord")
{
grd_Benefits.SelectedIndex = int.Parse(e.CommandArgument.ToString());
frm_Benefit.ChangeMode(FormViewMode.ReadOnly);
Page.ClientScript.RegisterStartupScript(this.GetType(), "ShowFormViewView", "<script type=\"text/javascript\">showFormViewView();</script>");
}
else if (e.CommandName == "NewRecord")
{
frm_Benefit.ChangeMode(FormViewMode.Insert);
Page.ClientScript.RegisterStartupScript(this.GetType(), "ShowFormViewInsert", "<script type=\"text/javascript\">showFormViewInsert();</script>");
}
else if (e.CommandName == "EditRecord")
{
grd_Benefits.SelectedIndex = int.Parse(e.CommandArgument.ToString());
frm_Benefit.ChangeMode(FormViewMode.Edit);
Page.ClientScript.RegisterStartupScript(this.GetType(), "ShowFormViewEdit", "<script type=\"text/javascript\">showFormViewEdit();</script>");
}
}
Seeing your Markups and taking it as it is, the issue is with the FormView.
You have not set the AllowPaging="true" for FormView. So every time the FormView displays, it will just Display only the First Row of all the data records retrieved.
[ Note that Form View displays only one single record at a time ].
So every time you are selecting 'Edit' in Form View, you are actually editing the First record only ( which is indeed the First row of GridView).
Set the AllowPaging property to true for FormView.
<asp:FormView ID="frm_Benefit" runat="server" DataKeyNames="CustomerID"
DataSourceID="sds_Benefits" OnItemUpdated="frm_Benefit_ItemUpdated"
AllowPaging="true">
After that using the Paging index, navigate to any record you want to change and edit it as seen below.

ModalPopupExtender inside DataList show up onclick of ImageButton inside datalist item

I have a datalist filled with items from database and each item has an image button onclick of which I want to show a modalpopup containing the Description of the particular Item. Below is what I have dine so far but fail:
<asp:DataList ID="DataList1" runat="server" RepeatColumns="3" RepeatDirection="Horizontal" Width="100%">
<ItemTemplate>
<table width="228px">
<tr>
<td width="20px" > </td>
<td width="160px" align="center">
<asp:Label ID="pID" runat="server" Visible="false" Text='<%# Eval("id") %>'></asp:Label><asp:Label ID="Label1" runat="server" Text='<%# Eval("ProductName") %>' Font-Size="8pt" ForeColor="#336699" Width="100%" />
</td>
<td align="right">
<asp:ImageButton ID="SpecificBtn" ImageUrl="images/SmallCallout.png" OnClick="SpecificBtn_Click" CommandArgument='<%# Eval("Id") %>' runat="server" />
<ajaxToolkit:ModalPopupExtender ID="mdl" runat="server" PopupControlID="pnl" TargetControlID="SpecificBtn" Enabled="True" CancelControlID="btn" DropShadow="true" Drag="True" ></ajaxToolkit:ModalPopupExtender>
</td>
</tr>
</table>
<asp:Image ID="Image1" runat="server" ImageUrl='<%# "GetImage.aspx?id=" + Eval("id") %>' /><br />
<table align="center" style="position:relative; left:2px;" cellspacing="0" cellpadding = "0" width="228px">
<tr>
<td style="background-color:Black;" colspan=2>
<div >
<asp:Image ID="Image2" ImageUrl="/images/cart.png" runat="server" />
<asp:Button CommandName="AddToCart" CssClass="anchor" OnClick="addProduct" CausesValidation="false" CommandArgument='<%# Eval("Id") %>' ID="addToHire" runat="server" Text=" Add To Cart " BackColor="Black" BorderColor="Black" BorderStyle="None" ForeColor="#F8CD20" Height="24" Font-Bold="true" Font-Size="10" />
</div>
</td>
</tr>
</table>
<br /><br />
</ItemTemplate>
<ItemStyle CssClass="dataListItemStyle" HorizontalAlign="Center" VerticalAlign="Top" />
</asp:DataList>
<asp:Panel ID="pnl" runat="server" visible="false" Width="300px" Height="300px" BackColor="red">
<asp:Label ID="Label2" runat="server" Text="Specification"></asp:Label>
<asp:Label ID="Label3" runat="server" Text='<%# Eval("Specification") %>'></asp:Label>
<asp:Button ID="btn" runat="server" Text="cancel" />
</asp:Panel>
Code behind:
protected void SpecificBtn_Click(object sender, EventArgs e)
{
DataListItem dli = (DataListItem)((ImageButton)sender).Parent;
ModalPopupExtender ModalPopupExtender2 = (ModalPopupExtender)dli.FindControl("mdl");
pnl.Visible = true;
ModalPopupExtender2.Show();
}
Can somebody tell me how to acheive this and where I went wrong. Thanks.
use jquery..
function onimagebuttonclick()
{
$find('BehaviorIDofmodelpopupextender').show();
}
i hope this solves your problem..

Object Data Source not filtering my listview?

I'm trying to filter a listview, using a textbox control via an object datasource. However when I add anything nothing happens . I've created something similar before and all the codes pretty much the same. anyone got any ideas' what I'm missing ??
<asp:TextBox ID="namefilter" runat="server" />
<asp:Button ID="button1" runat="server" />
<asp:ListView DataSourceID="DataSource" ID="ListView1" runat="server" DataKeyNames="ID" InsertItemPosition="LastItem">
<LayoutTemplate>
<table cellspacing="1" cellpadding="1" border="0" bgcolor="#6699cc" width="100%" >
<tr>
<th> </th>
<th> </th>
<th><span class="Caption1">Name</span></th>
<th><span class="Caption1">Phone</span></th>
</tr>
<tr id="itemPlaceholder" runat="server" />
</table>
</LayoutTemplate>
<ItemTemplate>
<tr style="background-color: White;" >
<td><asp:LinkButton ID="btnEdit" runat="server" Text="Edit" CommandName="Edit" /></td>
<td><asp:LinkButton ID="btnDelete" runat="server" Text="Delete" CommandName="Delete" /></td>
<td align="center"><%# DataBinder.Eval(Container, "DataItem.Name")%> </td>
<td align="center"><%# DataBinder.Eval(Container, "DataItem.Phone")%> </td>
</tr>
</ItemTemplate>
<EditItemTemplate>
<tr style="background-color: White;" >
<td><asp:LinkButton ID="btnSave" runat="server" Text="Save" CommandName="Update" /></td>
<td><asp:LinkButton ID="btnCancel" runat="server" Text="Cancel" CommandName="Cancel" /></td>
<td><asp:TextBox ID="EditName" runat="server" Text='<%# Bind("Name") %>' /></td>
<td><asp:TextBox ID="EditPhone" runat="server" Text='<%# Bind("Phone") %>' /></td>
</tr>
</EditItemTemplate>
<InsertItemTemplate>
<tr bgcolor="#6699cc" >
<td><asp:LinkButton ID="InsertButton" CommandName="Insert" runat="server" Text="Insert" ValidationGroup="add" CssClass="Caption1" /> </td>
<td><asp:LinkButton ID="CancelButton" CommandName="Cancel" runat="server" Text="Cancel" CausesValidation="false" CssClass="Caption1" /></td>
<td> <asp:TextBox ID="InsertName" runat="server" Text='<%# Bind("Name") %>' ValidationGroup="insert" /> </td>
<td><asp:TextBox ID="InsertPhone" runat="server" Text='<%# Bind("Phone") %>' /> </td>
</InsertItemTemplate>
</asp:ListView>
<asp:LinqDataSource
ContextTypeName="assembly"
EnableUpdate="true"
EnableDelete="true"
EnableInsert="true"
ID="DataSource"
OrderBy="Email desc"
runat="server"
TableName="Contacts"
AutoSort="true"
>
<whereParameters>
<asp:ControlParameter ControlID="emailfilter" Name="Email" PropertyName="Text" Type="String" ConvertEmptyStringToNull="false" />
</whereParameters>
</asp:LinqDataSource>
d'oh, forgot to add the sort by where clause in the linqdatasource.. 3 hours wasted!!!
<asp:LinqDataSource
ContextTypeName="Immediacy.VS.Plugins.DBML.VisitScotlandDataContext"
EnableUpdate="true"
EnableDelete="true"
EnableInsert="true"
ID="DataSource"
OrderBy="Email desc"
runat="server"
TableName="Contacts"
AutoSort="true"
Where='(#Email == null) || (Email == #Email)'
>

Categories