how to visible false edit commandbutton inside Gridview - c#

I am working on an asp.net application. In which i have added a commandbutton which is used to edit the row. My Gridview is
<asp:GridView ID="gvCustomerPaymentDetails" runat="server"
AutoGenerateColumns="false" ShowFooter="true" OnRowEditing="EditPayment"
OnRowUpdating="UpdatePayment" OnRowCancelingEdit="CancelEdit"
CssClass="table table-bordered table-hover" Style="margin-left: 5px; margin-right: 5px;">
<Columns>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:Label ID="lblID" runat="server" Text='<%# Eval("ID")%>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="2%" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Pay Amount">
<ItemTemplate>
<asp:Label ID="lblPayAmount" runat="server" Text='<%# Eval("Pay_Amount")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtPayAmount" runat="server" style="width:100%" Text='<%# Eval("Pay_Amount")%>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtPayAmount" style="width:100%" runat="server"></asp:TextBox> </FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Action">
<ItemTemplate>
<asp:LinkButton ID="linkPayNow" runat="server" Text="Pay Now" CommandArgument='<%#Eval("ID") %>' CommandName="Pay"></asp:LinkButton>
<asp:Label ID="txtStatus" runat="server" Text="Paid" Style="margin-left: 20px;" Visible="false"></asp:Label>
<asp:LinkButton ID="lnkRemove" runat="server" CommandArgument='<%# Eval("ID")%>' OnClientClick="return confirm('Do you want to delete?')"
Text="Delete" OnClick="DeletePayment"></asp:LinkButton>
</ItemTemplate>
<FooterTemplate>
<asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="AddNewPayment" CommandName="Footer" />
</FooterTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" />
</Columns>
<EmptyDataTemplate>
<tr>
<th scope="col">Pay Amount
</th>
<th scope="col"></th>
</tr>
<tr>
<td>
<asp:TextBox ID="txtPayAmount" runat="server" />
</td>
<td>
<asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="AddNewPayment" CommandName="EmptyDataTemplate" />
</td>
</tr>
</EmptyDataTemplate>
</asp:GridView>
I have a Label "lblStatus" which will be either 0 or 1. If its value is 1, then i want to visible false the Update and edit commandbutton of that specific row. I have searched a lot but not found any perfect solution. Please help me someone.

In this case, I would convert the CommandField to TemplateFields. To do so, you will click on the smart tag in the gridview in design view. What this will do is change the commandfields to linkbuttons, where you can set it to false in the behind code. You will want to give your new linkbuttons proper ID's so you can find them in the behind code. Then you will create a databound method.. like..
protected void GridView1_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
//Code Here to Disable button. I'd use a Foreach loop like this.
foreach(GridViewRow gvr in GridView1.Rows)
{
Label label = ((Label)gvr.FindControl("label"));
LinkButton edit = ((LinkButton)gvr.FindControl("edit"));
if (label.Text == 1)
{
edit.Visible = false;
}
}
}
Hopefully this puts you in the right direction. I think this should do the trick.

Related

checkbox itemtemplate not updating into an ASP GridView

working on an ASP Gridview querying a SQL server base.
<asp:Content ID="i_cttContenu" runat="server" ContentPlaceHolderID="i_cphContenu">
<asp:SqlDataSource ID="i_sdsGvOption" runat="server" ConnectionString="<%$ ConnectionStrings:... %>"
SelectCommand=" SELECT * FROM MyTable " SelectCommandType="Text"
UpdateCommand="UPDATE MyTable SET [name] = #name, prenom = #prenom, isAlive = #isAlive WHERE idWsgProgramOption = #idWsgProgramOption" UpdateCommandType="Text"
</asp:SqlDataSource>
<asp:UpdatePanel ID="i_up" runat="server">
<ContentTemplate>
<asp:GridView ID="i_gvOption" runat="server" AutoGenerateColumns="False" DataKeyNames="idWsgProgramOption"
DataSourceID="i_sdsGvOption" EnableModelValidation="True">
<Columns>
<asp:CommandField ButtonType="Image" CancelImageUrl="~/....gif"
CancelText="Annuler" EditImageUrl="~/....gif"
EditText="Update" HeaderText="M" UpdateImageUrl="~/....gif"
UpdateText="Save">
</asp:CommandField>
<asp:TemplateField HeaderText="Nom" SortExpression="name">
<ItemTemplate>
<asp:HyperLink ID="i_hlOption" runat="server" NavigateUrl='<%# Eval("idWsgProgramOption", "~/myURL") %>'
Text='<%# Eval("name") %>' />
</ItemTemplate>
<EditItemTemplate>
<table >
<tr>
<td >
<asp:TextBox ID="i_tbNom" runat="server" Text='<%# Bind("name") %>' />
</td>
</tr>
</table>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="prenom" SortExpression="prenom">
<ItemTemplate>
<asp:HyperLink ID="i_hlprenom" runat="server" NavigateUrl='<%# Eval("prenom", "~/myURL") %>'
Text='<%# Eval("prenom") %>' />
</ItemTemplate>
<EditItemTemplate>
<table >
<tr>
<td >
<asp:TextBox ID="i_tbprenom" runat="server" Text='<%# Bind("prenom") %>' />
</td>
</tr>
</table>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Obligatoire" >
<ItemTemplate>
<asp:CheckBox ID="CB_id1" runat="server" />
</ItemTemplate>
<EditItemTemplate>
<asp:CheckBox ID="CB_id2" runat="server" />
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
The update() method is not invoked when I click the update button (first column). If I add an onUpdating event into the datasource, the corresponding method is never invoked.
The code causing the issue is definitely the checkbox.
If I remove from the datasource update query:
, isAlive = #isAlive
and only set:
UPDATE MyTable SET [name] = #name, prenom = #prenom, isAlive = #isAlive WHERE idWsgProgramOption = #idWsgProgramOption;
Then it updates fine (except the isAlive field of course).
I am 100% sure that the isAlive field exists into the base (bit type).
So it looks that my problem comes from this bloc:
<asp:TemplateField HeaderText="Obligatoire" >
<ItemTemplate>
<asp:CheckBox ID="CB_id1" runat="server" />
</ItemTemplate>
<EditItemTemplate>
<asp:CheckBox ID="CB_id2" runat="server" />
</EditItemTemplate>
</asp:TemplateField>
Is there anything obvious that I missed??
Also, this is the simplified code, but if I set the "checked" property to the checkbox and correctly binds, the rows checkbox is correctly field. So the issue does not affect the select but only the update.
Try a button column instead so you can specify the specific command names.
<asp:ButtonField ButtonType="Link" Text="Update" CommandName="Update" />
<asp:ButtonField ButtonType="Link" Text="Delete" CommandName="Delete" />
Take action based on e.CommandName.

I need to use DataList inside another DataList

<asp:DataList ID="DataListDziennik" runat="server"
DataSourceID="SqlDataSourcePrzedmioty">
<ItemTemplate>
<asp:Label ID="LabelPrzedmiot" runat="server" Text='<%# Eval("przedmiot") %>' />
...
<asp:DataList ID="DataListOceny" runat="server"
DataSourceID="SqlDataSourceOceny"
RepeatDirection="Horizontal"
OnItemCommand="DataListOceny_ItemCommandOceny"
OnEditCommand="DataListOceny_EditCommandOceny">
<EditItemTemplate>
<asp:TextBox ID="TextBoxOcena" runat="server" Text='<%# Bind("lista") %>' />
<td><asp:Button ID="ButtonZapisz" CommandName="Update" runat="server" Text="Zapisz" /></td>
</EditItemTemplate>
<ItemTemplate>
<asp:TextBox Width="20" ID="TextBoxOcena" ReadOnly="true" Text='<%# Eval("lista") %>' runat="server"></asp:TextBox>
<td><asp:Button ID="ButtonEdytuj" CommandName="Edit" runat="server" Text="Edytuj" /></td>
</ItemTemplate>
</asp:DataList>
</td>
</ItemTemplate>
When I write this in code behind:
protected void DataListOceny_EditCommand(object source, DataListCommandEventArgs e)
{
DataListOceny.EditItemIndex = e.Item.ItemIndex;
DataListOceny.DataBind();
}
Visual Studio tells me that DataListOceny does not exist in current content. I just want to be able edit items on DataListOceny after clicking the "edit" button, please give me full code in code behind part...
for edit DataListOceny .
What you asking is same like the below thread.. You need to find the DataListOceny Control on each row of DataListDziennik to bind it
Using FindControl() to find control
https://msdn.microsoft.com/en-us/library/486wc64h%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396
And Here is some good example on the same
http://www.c-sharpcorner.com/UploadFile/rohatash/nested-datalist-in-Asp-Net/
http://surecode.net/DevCentre/NestedDataList/NestedDataList.aspx

Mess table in GridView and run time error

I want to create a table as below to interact with database to retrieve, delete, update data.
My output in design view is very messy. And when i try to run the page i receive the following error:
Control 'GridView1' of type 'GridView' must be placed inside a form tag with runat=server.
Please advise me for enhancement based on below data table and my coding.
.aspx:
<div class="modal-bg">
<asp:GridView ID="GridView1" runat="server" EnableEventValidation="false"
BackColor="White" BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px"
CellPadding="4" Width="426px">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
UsernameLast Login Status
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("UserName") %>'></asp:Label>
</ItemTemplate>
<HeaderTemplate>
Active Role
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("ActiveRole") %>'></asp:Label>
</ItemTemplate>
<HeaderTemplate >
Full name
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Eval("FullName") %>'></asp:Label>
</ItemTemplate>
<HeaderTemplate>
Email Address
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Eval("EmailAddress") %>'></asp:Label>
</ItemTemplate>
<HeaderTemplate>
Last Login Status
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Eval("Last Login Status") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<button id="newuserbutton" onclick="return newuserbutton_onclick()">
Create New User</button>
Thank you for helps.
Did you actually try to place your control inside the <form runat="server"></form> tags?
Surround your html code with tag => <form runat="server"> ... </form>

ASP GridView Problem

Im working on the front end development for a website built in .net, This is the first time I've done this and I'm having trouble trying to alter a table.
The code which outputs my table is...
<asp:GridView ID="GV_Concepts" runat="server" AutoGenerateColumns="False" DataKeyNames="ConCatID"
BorderStyle="None" GridLines="None" ShowHeader="False" BorderWidth="0px" CssClass="DashBoard_Concepts">
<Columns>
<asp:TemplateField HeaderText="Catalog">
<ItemTemplate>
<asp:Label ID="LB_Cata" runat="server" Text='<%# Bind("ConCatalog") %>'></asp:Label>
</ItemTemplate>
<HeaderStyle CssClass="col-b" />
</asp:TemplateField>
<asp:TemplateField HeaderText=" Concept Version" ItemStyle-Width="" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<div class="conceptstd">
<asp:DataList ID="DataList1" runat="server" RepeatColumns="3" BorderStyle="None"
DataSource='<%# Bind("DS_Version")%>'>
<ItemTemplate>
<asp:HyperLink ID="HL_ConcLoc" runat="Server" CssClass="linkage" NavigateUrl='<%# Bind("FileName") %>'
Target="_blank" Text='<%# Bind("Ver") %>'></asp:HyperLink>
<asp:LinkButton ID="LB_remove" runat="server" CommandArgument='<%# Bind("ConceptID") %>'
OnClick="LB_removecon_Click" CssClass="link-btn">Remove</asp:LinkButton>
<asp:LinkButton ID="LB_sign" runat="server" CommandArgument='<%# Bind("ConceptID") %>'
OnClick="LB_signcon_Click" CssClass="sign-off-btn" Visible='<%# SignedCheck(DataBinder.Eval(Container.DataItem,"SignOff")) ?true:false %>'>Sign Off</asp:LinkButton>
<asp:Literal ID="Lit_SignedCon" Visible='<%# SignedCheck(DataBinder.Eval(Container.DataItem,"SignOff")) ?false:true %>'
runat="server"><b>Signed Off</b></asp:Literal>
</ItemTemplate>
</asp:DataList>
</div>
</ItemTemplate>
<HeaderStyle CssClass="col-c" />
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
Currently no concepts
</EmptyDataTemplate>
</asp:GridView>
The html equivalent of this is something similar too...
<table>
<tr>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
</table>
However I need 5 columns not 2, I've tried reading up on the syntax but thought I'd ask here also, Thanks for any help.
I'm not exactly sure how your dataset is structured, but do you need that datalist in there or can you just put the controls in their own ItemTemplate in the GridView? This would give you 5 columns:
<asp:GridView ID="GV_Concepts" runat="server" AutoGenerateColumns="False" DataKeyNames="ConCatID"
BorderStyle="None" GridLines="None" ShowHeader="False" BorderWidth="0px" CssClass="DashBoard_Concepts">
<Columns>
<asp:TemplateField HeaderText="Catalog">
<ItemTemplate>
<asp:Label ID="LB_Cata" runat="server" Text='<%# Bind("ConCatalog") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink ID="HL_ConcLoc" runat="Server" CssClass="linkage" NavigateUrl='<%# Bind("FileName") %>'
Target="_blank" Text='<%# Bind("Ver") %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LB_remove" runat="server" CommandArgument='<%# Bind("ConceptID") %>'
OnClick="LB_removecon_Click" CssClass="link-btn">Remove</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LB_sign" runat="server" CommandArgument='<%# Bind("ConceptID") %>'
OnClick="LB_signcon_Click" CssClass="sign-off-btn" Visible='<%# SignedCheck(DataBinder.Eval(Container.DataItem,"SignOff")) ?true:false %>'>Sign Off</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Literal ID="Lit_SignedCon" Visible='<%# SignedCheck(DataBinder.Eval(Container.DataItem,"SignOff")) ?false:true %>'
runat="server"><b>Signed Off</b></asp:Literal>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
Currently no concepts
</EmptyDataTemplate>
</asp:GridView>
Of course, this ruins your binding to your datasource, but I'm not sure if I can accurately help you with fixing that :(
the number of column is automatically generated according to asp:templatefield
when you have two templatefield, the only two column will be generated, and the number
of rows depend on data.
so if you need 5 columns you have to put 5 templatefield inside the girdiview
Why do you "need" 5 columns? For layout? Perhaps a GridView isn't the right solution given what you're trying to accomplish. Could a Repeater that generates your content accomplish the same thing?

GridView CSS problem. How to add CSS to header tr

Can i in GridView add css to only first(headr) tr. I know how to add css to th inside header trr but i don't know if is possible to add css to trr?
GRIDVIEW:
<asp:GridView ID="gwCompanies" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="Ime">
<ItemTemplate>
<asp:Image ID="imgServiceOpen" runat="server" ToolTip="Restavracija sprejema naročila" ImageUrl="~/Images/cheff-icon.png" Visible='<%# Convert.ToBoolean(Eval("ServiceOpen")) %>' />
<asp:HyperLink ID="hlEdit" runat="server" NavigateUrl='<%# GetOrderArticlesUrl(Eval("CompanyId")) %>'>
<asp:Label ID="lblName" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Naslov">
<ItemTemplate>
<asp:Label ID="lblFullAddress" runat="server" Text='<%# GetFullCompanyAddress(Container.DataItem) %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
<asp:Label ID="lblEmptyCompaniesGrid" runat="server" Text="Ne najdem restavracij!"></asp:Label>
</EmptyDataTemplate>
</asp:GridView>
output i want
<table id="rest_list">
<tr class="top_tab">
<td class="top_tab_title">Ime</td>
<td class="top_tab_title">Naslov</td>
</tr>
<tr>
<td class="rest_name">Murka</td>
<td>Točen naslov pizzerije</td>
</tr>
<tr>
<td class="rest_name">Lastoria</td>
<td>Točen naslov pizzerije</td>
</tr>
<tr>
<td class="rest_name">Skok</td>
<td>Točen naslov pizzerije</td>
</tr>
<tr>
<td class="rest_name">Tara</td>
<td>Točen naslov pizzerije</td>
</tr>
</table>
You can add a class with the HeaderStyle property, like this:
<asp:GridView ID="gwCompanies" runat="server" AutoGenerateColumns="false">
<HeaderStyle CssClass="top_tab" />
Or in the GridView tag, like this:
<asp:GridView ID="gwCompanies" runat="server" HeaderStyle-CssClass="top_tab" AutoGenerateColumns="false">
Not sure I understand correctly, but you should use a HeaderTemplate to define your header row, e.g:
<asp:TemplateField HeaderText="Ime">
<ItemTemplate>
...
</ItemTemplate>
<HeaderTemplate>
put here the content for the header row of this column
</HeaderTemplate>
</asp:TemplateField>
Then you can apply whatever style you want to your header.

Categories

Resources