How to show and hide textbox in gridview on link button click - asp.net

I have gridview,which has approve and reject button.When click on reject button i need to show textbox for comments else no.after enetering comments i have save it in data base.How to do this can any one help me
here is my code:
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False" BackColor="White"
BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="4" ForeColor="Black"
GridLines="Horizontal" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Industrial Zone">
<ItemTemplate>
<asp:Label runat="server" ID="lblindzone" Text='<%# Eval("indzone") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="District">
<ItemTemplate>
<asp:Label runat="server" ID="lbldstr" Text='<%# Eval("dstr")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Industrial Area">
<ItemTemplate>
<asp:Label runat="server" ID="lblnmindar" Text='<%# Eval("nmindar")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Plot Number">
<ItemTemplate>
<asp:Label runat="server" ID="lblplno" Text='<%# Eval("plno")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Approve/Reject">
<ItemTemplate>
<asp:LinkButton ID="lnkApproved" runat="server" Text="Approve" OnClick="lnkApproved_Click"><img src="images/approve.png" style="width:20px;height:20px; margin:5px" title="Approve"/></asp:LinkButton>
<asp:LinkButton ID="lnkReject" runat="server" Text="Reject" OnClick="lnkReject_Click" OnClientClick="return showandhide(this)"><img src="images/reject.png" style="width:16px;height:16px; margin:5px" title="Reject"/></asp:LinkButton>
<asp:LinkButton ID="lnkviewdetails" runat="server" Text="View Details" OnClick="lnkviewdetails_Click"><img src="images/viewdetails.png" style="width:20px;height:20px; margin:5px" title="Details" /></asp:LinkButton>
<asp:TextBox ID="txtcomment" runat="server" style="display:none"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Status">
<ItemTemplate>
<asp:Label runat="server" ID="lblstatus" Text='<%# Eval("status")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

I am assuming you are doing it with only C# and no Javascript. to view or hide a specific textbox you should trigger a RowCommand Event of GridView:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "viewhide")
{
GridViewRow gvr = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
TextBox tbComments = ((TextBox)gvr.FindControl("txtcomment"));
tbComments.Visible = true;
}
}
And this would be your ASPX Codebehind
<asp:LinkButton ID="lnkReject" runat="server" Text="Reject" OnClick="lnkReject_Click" CommandName="viewhide" OnClientClick="return showandhide(this)"><img src="images/reject.png" style="width:16px;height:16px; margin:5px" title="Reject"/></asp:LinkButton>
EDIT: Now You've changed the tag from C# to VB.NET which is not good. because i consumed all my time writing a c# code!

Related

RowUpdating not firing in EditItemTemplate

I'm binding an array to GridView. Below is my template field and it doesn't fire RowUpdating when I click on update.
<asp:TemplateField HeaderText="Role">
<EditItemTemplate>
<asp:TextBox runat="server" Text='<%# Container.DataItem.ToString() %>' ID="txtEditRole"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<%# Container.DataItem.ToString() %>
</ItemTemplate>
</asp:TemplateField>
This happened after making the field to TempleteField. Earlier the field was like below.
<asp:BoundField DataField="!" HeaderText="Role" />
Make sure you specify OnRowUpdating="gv_RowUpdating" event and also change FieldName in <%#Eval("Role") %>, see this example:
.aspx page
<asp:GridView ID="gv" runat="server" DataKeyNames="Id" AutoGenerateColumns="false" OnRowEditing="gv_RowEditing"
OnRowUpdating="gv_RowUpdating" OnRowCancelingEdit="gv_RowCancelingEdit" OnRowCommand="gv_RowCommand" OnRowDeleting="gv_RowDeleting">
<Columns>
<asp:TemplateField>
<EditItemTemplate>
<asp:LinkButton ID="lbtnUpdate" runat="server" CommandName="Update" Text="Update" />
<asp:LinkButton ID="lbtnCancel" runat="server" CommandName="Cancel" Text="Cancel" />
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="lbtnEdit" runat="server" CommandName="Edit" Text="Edit" />
<asp:LinkButton ID="lbtnDelete" runat="server" CommandName="Delete" Text="Delete" OnClientClick="return confirm('Are you sure you want to delete this record?')" CausesValidation="false" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Role">
<EditItemTemplate>
<asp:TextBox ID="txtEditRole" runat="server" Text='<%#Eval("Role") %>' />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblRole" runat="server" Text='<%#Eval("Role") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
.aspx.cs
protected void gv_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//your code here..
}
To check complete article, checkout insert, update, delete gridview data example in asp.net.

ASP.NET DataGrid Select ItemTemplate By Condition

This is My DataGrid:
<asp:DataGrid ID="dgAll" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:TextBox ID="txtB" runat="server" Text='<%#Eval("Value")%>' ></asp:TextBox>
</ItemTemplate>
<ItemTemplate>
<asp:DropDownList ID="drdL" runat="server" DataSource='<%#GetComboData(Eval("Value"))%>'></asp:DropDownList>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
As you see I have a TextBox and a DropDownList, But I need just one of them in each row, So the question is how can I select ItemTemplate by condition, assume if Eval("Type") == "TextBox") I need TextBox and if Eval("Type") == "DropDown") I need DropDown in that Row. Does any one have any idea about this?
You can use both the controls in the same template field and use code as below
<asp:DataGrid ID="dgAll" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:TextBox ID="txtB" runat="server" Text='<%#Eval("Value")%>'
Visible ='<%# Eval("Type").ToString()=="TextBox"%'>
</asp:TextBox>
<asp:DropDownList ID="drdL" runat="server"
DataSource='<%#GetComboData(Eval("Value"))%>'
Visible ='<%# Eval("Type").ToString()!="TextBox"%'>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
why do you use couple asp:TemplateColumn, you can use too like that,
<asp:TemplateColumn>
<ItemTemplate>
<asp:TextBox ID="txtB" runat="server" Text='<%#Eval("Value")%>' Visible="false" ></asp:TextBox>
<asp:DropDownList ID="drdL" runat="server" Visible="false"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateColumn>
and in the Grid's ItemDataBound event
protected void dgAll_ItemDataBound(object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemIndex == -1) return;
if (e.Item.ItemIndex % 2 == 0)
e.Item.FindControl("drdL").Visible = true;
else
e.Item.FindControl("txtB").Visible = true;
}

Sorting asp.net GridView with SPDataSource

I would like to sort my GridView by clicking on the column's header. So far I declared a SPDataSource
<SharePoint:SPDataSource
runat="server"
ID="SPdsInventarGrid"
DataSourceMode="List"
UseInternalName="true"
Scope="Recursive"
SelectCommand="<View><Query><Where><Or><Neq><FieldRef Name='EBS_x002e_CN_x002e_Inventar_x00210' /><Value Type='Text'>Open</Value></Neq><IsNull><FieldRef Name='EBS_x002e_CN_x002e_Inventar_x0028' /></IsNull></Or></Where></Query><ViewFields><FieldRef Name='Title'/><FieldRef Name='ID'/><FieldRef Name='EBS_x002e_CN_x002e_Inventar_x0028'/><FieldRef Name='EBS_x002e_CN_x002e_Inventar_x00210'/><FieldRef </ViewFields></View>">
<SelectParameters>
<asp:Parameter Name="ListName" DefaultValue="Inventar" />
</SelectParameters>
</SharePoint:SPDataSource>
Then I created my grid, based on the datasource
<asp:GridView Visible="true" Width="100%"
ID="gvInventar"
AutoGenerateColumns="false" runat="server" CellPadding="2" AllowPaging="false" AllowSorting="true" OnSorted="gvInventar_Sorted" OnSorting="gvInventar_Sorting" GridLines="None" DataSourceID="SPdsInventarGrid" OnRowCommand="gvInventar_RowCommand">
<Columns>
<asp:CommandField ButtonType="Image" ShowEditButton="true" EditImageUrl="/_layouts/images/edit.gif" UpdateImageUrl="/_layouts/images/save.gif" CancelImageUrl="/_layouts/images/delete.gif" />
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:Label Text='<%# Bind("ID") %>' runat="server" ID="lblIdProduct"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Title" >
<ItemTemplate>
<asp:Label Text='<%# Bind("Title") %>' runat="server" ID="lblTitleProduct" ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Product" SortExpression="Product">
<ItemTemplate>
<asp:Label Text='<%# Bind("EBS_x002e_CN_x002e_Inventar_x0028") %>' runat="server" ID="lblProduct" "></asp:Label>
</ItemTemplate>
</Columns>
</asp:GridView>
Now, I have this function where I wanted to get the current Query as a string and add <OrderBy>(my field) before <Query> but i get an error saying that the DataSource has been already declared (which is true because i want to be declared in asp)
protected void gvInventar_Sorting(object sender, GridViewSortEventArgs e)
{
SPQuery q = new SPQuery();
q.Query = SPdsInventarGrid.SelectCommand;
switch (e.SortExpression)
{
case "Product":
if (e.SortDirection == SortDirection.Ascending)
{
gvInventar.DataSource = q.Query;
gvInventar.DataBind();
}
else
{
}
break;
}
}
Even though it's not working this way, I don't think modifying the Query is the solution.
Could anyone help me with this matter?
You don't need to write any custom code to sort the gridview by clicking the column headers, all you have to do is set the SortExpression property of the TemplateField to the required column name, e.g:
<Columns>
<asp:TemplateField HeaderText="ID" SortExpression="ID">
<ItemTemplate>
<asp:Label Text='<%# Bind("ID") %>' runat="server" ID="lblIdProduct" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Title" SortExpression="Title">
<ItemTemplate>
<asp:Label Text='<%# Bind("Title") %>' runat="server" ID="lblTitleProduct" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Product" SortExpression="EBS_x002e_CN_x002e_Inventar_x0028">
<ItemTemplate>
<asp:Label Text='<%# Bind("EBS_x002e_CN_x002e_Inventar_x0028") %>' runat="server"
ID="lblProduct" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
Now you can sort on all 3 columns:

How can I pass parameters to a Crystal Report from my GridView?

I have an existing SQL database and a ASP.NET application. My application has two existing GridViews and login functions. I also have an existing Crystal Report designed to automatically create a receipt from my SQL database. This is done by the user filling out 3 specific parameters, and the rest of the data (which are in parallel with those parameters) will automatically fill out the crystal report.
I want to create a print button in my GridView to automatically fill out the 3 parameters in the Crystal Report. This is an attempt to make my application more user friendly. In short, the user would push the print button in its new column in the GridView, and the 3 parameters would be automatically picked up and filled into the Crystal Report.
My parameters are: "EmpID", "KeyControl", and "ControlNumber". My crystal report label is "x.rpt"
Here is my GridView markup:
<asp:GridView ID="gridKeyAndBuildingInformation" runat="server" CssClass="style3"
AllowSorting ="True"
AutoGenerateColumns ="False"
AllowPaging="True"
DataKeyNames="KeyRefId"
OnRowCancelingEdit="gridKeyAndBuildingInformation_RowCancelingEdit"
onPageIndexChanging="gridKeyAndBuildingInformation_PageIndexChanging"
OnRowDataBound="gridKeyAndBuildingInformation_RowDataBound"
OnRowEditing="gridKeyAndBuildingInformation_RowEditing"
OnRowUpdating="gridKeyAndBuildingInformation_RowUpdating"
OnRowCommand="gridKeyAndBuildingInformation_RowCommand"
ShowFooter="True"
OnRowDeleting="gridKeyAndBuildingInformation_RowDeleting"
AlternatingRowStyle-BackColor="#EFEFEF"
EditRowStyle-VerticalAlign="Top"
HeaderStyle-BackColor="#77b218"
OnSorting="gridKeyAndBuildingInformation_Sorting"
BackColor="#CCCCCC"
BorderColor="#999999"
BorderStyle="Solid"
BorderWidth="3px"
CellPadding="4"
EnableModelValidation="True"
ForeColor="Black"
CellSpacing="2">
<Columns>
<asp:TemplateField HeaderText ="EmpID" HeaderStyle-CssClass="HeaderText" sortexpression="EmpID">
<ItemTemplate>
<asp:Label ID="lblEmpID" runat="server" Text='<%# Eval("EmpID") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEmpID" runat="server" Text='<%# Eval("EmpID") %>' Width="50px"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtNewEmpID" runat="server" Width="50px"></asp:TextBox>
</FooterTemplate>
<HeaderStyle CssClass="HeaderText" />
</asp:TemplateField>
<asp:TemplateField HeaderText ="ControlNumber" HeaderStyle-CssClass="HeaderText" sortexpression="ControlNumber">
<ItemTemplate>
<asp:Label ID="lblControlNumber" runat="server" Text='<%# Eval("ControlNumber") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtControlNumber" runat="server" Text='<%# Eval("ControlNumber") %>' Width="50px"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtNewControlNumber" runat="server" Width="50px"></asp:TextBox>
</FooterTemplate>
<HeaderStyle CssClass="HeaderText" />
</asp:TemplateField>
<asp:TemplateField HeaderText ="KeyNumber" HeaderStyle-CssClass="HeaderText" sortexpression="KeyNumber">
<ItemTemplate>
<asp:Label ID="lblKeyNumber" runat="server" Text='<%# Eval("KeyNumber") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtKeyNumber" runat="server" Text='<%# Eval("KeyNumber") %>' Width="50px"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtNewKeyNumber" runat="server" Width="50px"></asp:TextBox>
</FooterTemplate>
<HeaderStyle CssClass="HeaderText" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit" ShowHeader="False">
<EditItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update" Text="Update"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="AddNew" Text="Add New"></asp:LinkButton>
</FooterTemplate>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField HeaderText="Delete" ShowDeleteButton="True" ShowHeader="True" />
<asp:ButtonField HeaderText="Print" ShowHeader="True" Text="Print" />
</Columns>
<EditRowStyle VerticalAlign="Top"></EditRowStyle>
<FooterStyle BackColor="#CCCCCC" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White"></HeaderStyle>
<PagerStyle BackColor="#CCCCCC" ForeColor="Black" HorizontalAlign="Left" />
<RowStyle BackColor="White" />
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
</asp:GridView>
You could handle the RowCommand event, which would fire when you click that delete button in your ButtonField column. Add this to the end of your GridView declaration markup (after "CellSpacing="2"" but before the ">"):
OnRowCommand="gridKeyAndBuildingInformation_RowCommand"
And then, in your code behind you would need something like this (this is C#, if you need VB.NET let me know - this question isn't tagged with a server-side language):
protected void gridKeyAndBuildingInformation_RowCommand(Object sender, GridViewCommandEventArgs e)
{
// Get your ID for the row you're on
int ID = Convert.ToInt32(e.CommandArgument);
// Get the row the button was clicked in
GridViewRow row = gridKeyAndBuildingInformation.Rows[ID];
// Get the values you need from that row
int EmpID = row.Cells[0];
int ControlNumber = row.Cells[1];
int KeyNumber = row.Cells[2];
// Use those numbers to make your call to the Crystal Report
// I don't know what this part would look like.
}
You can read more about the RowCommand event on MSDN: GridView.RowCommand Event

EditItemTemplate values not getting read properly

I am trying to build a simple GridView.
I have a Products table and it contains ProductID and ModelName.
I am using LINQ to SQL
My GridView code is
<asp:GridView ID="GridView1" runat="server" DataSourceID="objData" AutoGenerateColumns="false" AutoGenerateEditButton="true" DataKeyNames="ProductID,ModelName">
<Columns>
<asp:TemplateField HeaderText="Product ID">
<ItemTemplate>
<asp:Label runat="server" Text='<%#Eval("ProductID") %>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Model Name">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#Eval("ModelName") %>'/>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" Text='<%#Eval("ModelName") %>'/>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
and my datasource code is
<asp:ObjectDataSource ID="objData" runat="server" SelectMethod="GetAll" UpdateMethod="UpdateProduct"
DataObjectTypeName="TestLINQProto.tblProduct" TypeName="TestLINQProto.ProductsList"></asp:ObjectDataSource>
I have my update method written like this
[DataObjectMethod(DataObjectMethodType.Update,true)]
public void UpdateProduct(tblProduct product)
{
EshopDataAccess.UpdateProduct(product);//This will call a LINQ function to update the product
}
The issue is that the tblProduct that the UpdateProduct contain original values. It is not getting the updated values.
I think the issue is that you are using Eval method which only supports reading/fetching of data. Use Bind Method in your grid aspx code. Like this
<asp:GridView ID="GridView1" runat="server" DataSourceID="objData" AutoGenerateColumns="false" AutoGenerateEditButton="true" DataKeyNames="ProductID,ModelName">
<Columns>
<asp:TemplateField HeaderText="Product ID">
<ItemTemplate>
<asp:Label runat="server" Text='<%#Eval("ProductID") %>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Model Name">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#Eval("ModelName") %>'/>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" Text='<%#Bind("ModelName") %>'/>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

Resources