I am using a GridView to get data from a datasource.
I want to add a textbox at the end of each column in the GridView i.e at the footer
How do I do that?
Use FooterTemplate. Example:
<asp:TemplateField>
<ItemTemplate>
...
</ItemTemplate>
<FooterTemplate>
your textboxes go here
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
...
</ItemTemplate>
<FooterTemplate>
your textboxes go here
</FooterTemplate>
</asp:TemplateField>
You should understand the differences between the BoundField and the TemplateField class. The first is used to display fields as a text, while in the TemplateField you can customize the way you're going to display the information. So, you should use BoundField or TemplateField in a column (not one nested inside another as I think you're trying), in your case it has to be the TemplateField, because you want to customize the way your footer shows up. So, it should be something like this:
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblNumber" runat="server" Text='<%# Bind("Number")%>' />
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txb" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
Add TextBox in <FooterTemplate>
<asp:TemplateField HeaderText="UnitsInStock">
<ItemTemplate>
//your displaying control
</ItemTemplate>
<FooterTemplate>
<asp:TextBox id="tb1" Text="Text" runat="server" />
</FooterTemplate>
</asp:TemplateField>
Find footer control in OnRowDataBound event:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Footer)
{
TextBox tb1 = (TextBox)e.Row.FindControl("tb1");
//do your stuff
}
}
Related
Today i was come to an issue when i was deleting a record on basis of id from grid view and i used OnRowCommand event for this.
here is my gridview code :
<asp:GridView ID="gridShow" runat="server" AutoGenerateColumns="False" PageSize="5"
AllowPaging="true" ShowHeader="false" OnRowCommand="s_index" OnRowDeleting="gridShow_RowDeleting">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="txtID" runat="server" Text='<%#Eval("ID") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="15%">
<ItemTemplate>
<asp:TextBox ID="txtDescription" runat="server" Text='<%#Eval("RollNumber") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="txtFname" runat="server" Text='<%#Eval("FirstName") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="15%">
<ItemTemplate>
<asp:TextBox ID="txtLname" runat="server" Text='<%#Eval("LastName") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="15%">
<ItemTemplate>
<asp:TextBox ID="txtEmail" runat="server" Text='<%#Eval("Email") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="15%">
<ItemTemplate>
<asp:Label ID="lblStatus" runat="server"></asp:Label>
<asp:HiddenField ID="hdnStatus" runat="server" Value='<%#Eval("UserName") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lb" runat="server" Text="Delete" CommandName="delete" CommandArgument='<%#Eval("ID") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Here is my C# code :
protected void s_index(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "delete")
{
oSRegisterBLL.BLLdelete(Convert.ToInt32(e.CommandArgument));
gview();
}
}
protected void gridShow_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
}
I successfully did this task by adding OnRowDeleting event on my grid view and definition of that event on my page behind but when i removed this first time i have come to known and issue " ASP.datashow_aspx' does not contain a definition for 'gridShow_RowDeleting' and no extension method 'gridShow_RowDeleting' accepting a first argument of type 'ASP.datashow_aspx' could be found (are you missing a using directive or an assembly reference?) "
I am confused with that why to add OnRowDeleting event on grid view with onrowcommand event ?
why i am confused because if i did not do any work with this event then why to use this event?
is there any way to work with only onrowcommand event ? or adding onrowdeleting event is essential for deletion of records from gridview?
I want to clear my mind for this ?
Your aspx markup of the GridView has declared the event handler here:
OnRowDeleting="gridShow_RowDeleting"
So when you try to remove it from the coedebehind you'll get that exception. So simply remove the event-handler and you can remove it from the codebehind.
Edit
Having a Delete-button, or even a regular button in a GridView with a CommandName of delete(which is the case here), will automatically try to fire OnRowDeleting.
So you have to add the event handler even if you don't use it or you have to rename the CommandName to e.g. "DeleteUser", otherwise you get exceptions like "The GridView 'gridShow' fired event RowDeleting which wasn't handled".
I have a GridView that is made up of two database fields populated via a stored procedure and then three fields for user input (two checkbox controls and one textbox) when I click on the save button I can get the information from the three controls but nothing from the two that were populated via the database. How can I get the first two fields?
<asp:GridView ID="gvA1" runat="server" AutoGenerateColumns="false" DataKeyNames="CodeNo" AutoGenerateSelectButton="false" EnablePersistedSelection="True" Visible="false">
<Columns>
<asp:TemplateField Visible="false" >
<ItemTemplate>
<%#DataBinder.Eval(Container.DataItem, "CodeNo")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Wrap="true" ItemStyle-Width="400px" HeaderText="Violation">
<ItemTemplate>
<%#DataBinder.Eval(Container.DataItem, "CodeViolationCited") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="A1Accordion_cbPool" runat="server" Text="Pool:" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="A1Accordion_cbSpa" runat="server" Text="Spa:" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Additional Comments" Visible="true">
<ItemTemplate>
<asp:TextBox ID="A1Accordion_tb" runat="server" TextMode="MultiLine"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code Behind:
protected void SaveAndCollapseA1(object sender, EventArgs e)
{
//good stuff
CheckBox myCheckBox_1 = gvA1.Rows[0].FindControl("A1Accordion_cbPool") as CheckBox;
CheckBox myCheckBox_2 = gvA1.Rows[0].FindControl("A1Accordion_cbSpa") as CheckBox;
TextBox myTextBox = gvA1.Rows[0].FindControl("A1Accordion_tb") as TextBox;
//not so good stuff
String myString1 = gvA1.Rows[0].Cells[0].ToString();
String myString2 = gvA1.Rows[0].Cells[1].ToString();
}
I figured it out but I haven't been hear long enough to post the solution via the links but if you change the columns as so:
<asp:label ID="lblA1CodeNo" runat="server" text='<%# Eval("CodeNo") %>'></asp:label>
then the values are available...
Try:
<%# Bind("CodeViolationCited") %>
Instead of
<%#DataBinder.Eval(Container.DataItem, "CodeViolationCited") %>
Eval is one way, bind is two-way.
See Microsoft's documentation for data-binding expressions
I'm working on a C#/ASP 4.0 project where I'm trying to make a shopping cart application.
There is a GridView on my Products page that shows all of the items, and I want the user to be able to click an "Add to Cart" button field in this GridView which will, obviously, add an item to their cart.
I'm having issues actually setting an OnClick event for the gridview, though? That doesn't seem to be available in the Event menu in the Properties. Additionally, I can't seem to figure out how to get the specific row, either. I have method that does this...
int productID = Convert.ToInt32(GridView1.Rows[n].Cells[0].Text);
AddToCart(productID);
But I have no idea how to figure out n, or how to have this method get called when they click that ButtonField in the gridview.
You could do this:
First, add a template field to the gridview:
<asp:TemplateField HeaderText="Add to Cart">
<ItemTemplate>
<asp:Button id="bthAddToCart"
CommandArgument'<%#Eval("ProductID")%>'
OnClick="bthAddToCart_Click"
Text="Add to Cart"
runat="server"/>
</ItemTemplate>
</asp:TemplateField>
Now, add the handler for the Click event of the button:
protected void bthAddToCart_Click(object sender, EventArgs e)
{
Button button = (Button)sender;
int productID = Convert.ToInt32(button.CommandArgument);
AddToCart(productID);
}
You can use the template fields like this:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowCommand="GridView1_RowCommand">
<Columns>
<asp:TemplateField HeaderText="Header Text Here">
<ItemTemplate>
CONTROL TO SHOW COLUMN DATA
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Header Text Here">
<ItemTemplate>
CONTROL TO SHOW COLUMN DATA
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Header Text Here">
<ItemTemplate>
CONTROL TO SHOW COLUMN DATA
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Header Text Here">
<ItemTemplate>
CONTROL TO SHOW COLUMN DATA
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderStyle-Width="30px">
<ItemTemplate>
<asp:Button ID="btnAddToCart" runat="server" Text="Add To Cart" CommandName="AddToCart"
CommandArgument='<%# Eval("ProductID") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
No Data Found.
</EmptyDataTemplate>
</asp:GridView>
Then on your code behind:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "AddToCart")
{
int ProductID = Convert.ToInt32(e.CommandArgument);
AddToCart(ProductID);
}
}
Hope this helps! Good Luck!
Use the OnRowCommand event of the grid view. More details: here
You have to use OnRowCommand Event for Gridview. Use Following Code:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowCommand="GridView1_RowCommand">
<Columns>
<asp:TemplateField HeaderText="Header Text Here">
<ItemTemplate>
CONTROL TO SHOW COLUMN DATA
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderStyle-Width="30px">
<ItemTemplate>
<asp:Button ID="btnAddToCart" runat="server" Text="Add To Cart" CommandName="AddToCart"
CommandArgument='<%# Eval("ProductID") %>' />
</ItemTemplate>
</asp:TemplateField>
</asp:GridView>
In C# Code use following code:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Add To Cart")
{
int ProductID = Convert.ToInt32(e.CommandArgument);
AddToCart(ProductID);
}
}
I have the following markup in an ASP.NET page:
<asp:GridView ID="gv" runat="server" DataSourceID="ods" OnRowDataBound="gv_RowDataBound">
<columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="cb" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="tbx" runat="server"/>
</ItemTemplate>
</asp:TemplateField>
</columns>
</asp:GridView>
How can I validate the TextBox only if the CheckBox is checked?
I searched for similar situations, but couldn't find anything.
Thanks.
U can use RowCommand under which u can get the control status using int index = AuthorsGridView.EditIndex;
GridViewRow row = AuthorsGridView.Rows[index]; and then validate the text box
refer this http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridviewrow.aspx
I have a field in a details view shown below
<asp:BoundField DataField="DTMON_F" HeaderText="Monday Hours From: " InsertVisible="False"
ReadOnly="True" SortExpression="HOURS" Visible="false" />
<asp:TemplateField HeaderText="Monday Hours From: " SortExpression="HOURS">
<EditItemTemplate>
<uc1:TimePicker ID="tpMondayHours" runat="server"/>
</EditItemTemplate>
<InsertItemTemplate>
<%-- <uc1:TimePicker runat="server" ID="tpMondayHours" />--%>
<asp:TextBox ID="txtMondayHours" runat="server" Text='<%# Bind("DTMON_F") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="lblMondayHours" runat="server" Text='<%# Bind("DTMON_F") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
Before "DTMON_F" is binded to the view I want to cut the string that is returned...Where and how might I do this?
You can implement the OnDataBinding event for each control instead of doing the bind inline. This will give you the ability to do whatever you like with the data before assigning it to the control.
Example using your Label. The same could be applied to the TextBox:
<asp:Label ID="lblMondayHours" runat="server"
OnDataBinding="lblMondayHours_DataBinding"></asp:Label>
protected void lblMondayHours_DataBinding(object sender, System.EventArgs e)
{
Label lbl = (Label)(sender);
string yourValue = (int)(Eval("DTMON_F"));
// *** Do whatever you want with the value now
lbl.Text = yourValue;
}