I have an ASP.NET(2.0 using C#) web app, in which I have a gridview on a page(My_Page.aspx). The gridview gets its data from an Oracle database. I have a Hyperlinkfield in the gridview, which when clicked, postsback to the same page, but with details of the item clicked(using QueryString).
<asp:HyperLinkField DataNavigateUrlFields="ID"
DataNavigateUrlFormatString="My_Page.aspx?id={0}"
DataTextField="NAME"
HeaderText="Item1"
SortExpression="NAME" />
I wanted to know how to change the style of the row in which I clicked the Hyperlink.
Any Ideas?
Thank you.
First, a HyperLink(Field) does generally not post back, but in your case, requests the same page with new parameters.
To set a CSS class for a GridView row, use the RowCreated event to do something like this:
protected void GridView_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
if (e.Row.DataItem.ToString() == Request["id"])
e.Row.CssClass = "highlighted-css-class";
}
I suggest you use Javascript to achieve that.
Since you can't work with the linkbutton directly,
you must navigate in the DOM to the parent TR (TableRow) element and set its class property.
First, you add the onclick attribute (if not possible for HyperLinkField try a simple HyperLink or LinkButton)
Pass the control as a parameter to the Javascript function
onclick="selectRow(this)"
function selectRow(hyperlink) { ... }
I'll let you figure out how to navigate through the DOM to find the parent TR.
A few references:
domtables
domstructure
Hope it helps
My situation:
<asp:GridView ID="gvProjects" runat="server">
<SelectedRowStyle CssClass="current" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="cmdShowProject" runat="server"
OnCommand="cmdShowProject_Command"
CssClass="show-project" CommandName="select"
</ItemTemplate>
</asp:TemplateField>
SelectedRowStyle must be set (either through individual properties or through CssClass).
CommandName must be "select" for the Row style to change.
The GridView is in an UpdatePanel.
Related
i'm gonna keep it short and simple
I'm a software engineering student in the 12th grade and as my final project I have decided to make a website. What the website is about doesn't really matter. The problem is this:
In the picture attached there is a textbox inside a templatefield inside that gridview. I need to get the value that the user writes inside. After you write a value you press Purchase. I've looked at similar questions and none offered a working solution. What happens is the value just disappears. I find the right control with FindControl, but the value gets deleted somehow. How do I know I am at the right control? I went to the client side and added to the asp:TextBox the following:
Text="5"
This works perfectly, so I know it gets to the right control but something makes it disappear. My gridview is being populated by a DataSet that is two datasets combined, and I put the Merge command and the DataSource and DataBind are both in if (!this.IsPostBack). I am completely lost and have no idea what to do, Help is much appreciated.The Picture of the Gridview
All the controls in a GridView are accessible by searching the correct row with FindControl. For that you can send the row number as a CommandArgument and use that in code behind. So first start by using OnCommand instead on OnClick and set the CommandArgument on the aspx page.
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Purchase" OnCommand="Button1_Command" CommandArgument='<%# Container.DataItemIndex %>' />
</ItemTemplate>
</asp:TemplateField>
And then in code behind
protected void Button1_Command(object sender, CommandEventArgs e)
{
//get the rownumber from the command argument
int rowIndex = Convert.ToInt32(e.CommandArgument);
//find the textbox in the corrext row with findcontrol
TextBox tb = GridView1.Rows[rowIndex].FindControl("TextBox1") as TextBox;
//get the value from the textbox
try
{
int numberOfTickets = Convert.ToInt32(tb.Text);
}
catch
{
//textbox is empty or not a number
}
}
I feel like I'm missing something obvious here but I'm just not getting it. I have a gridview with a template field column where i have a button and a hidden field. I'm trying to get the reference to both the button and the hidden field on row databound, as both the button's label and commandargument etc change depending on the other row data.
If i place a breakpoint in the area indicated in the code below, I can see that the hidden field is being assigned to properly, but the button is not. What am I missing here?
GridView.aspx
<asp:GridView ID="gvCurrentQueueStatus" runat="server" AutoGenerateColumns="False"
OnRowDataBound="gvCurrentQueueStatus_RowDataBound">
<Columns>
<asp:TemplateField ItemStyle-HorizontalAlign="Center" HeaderText="Service Controls">
<ItemTemplate>
<asp:Button ID="btnSubmitCommand" runat="server" Text="Control" OnClick="btnSubmitCommand_Click" />
<asp:HiddenField ID="hdnQueueNumber" runat="server" Value='<%# Eval("ReportQueueNumber") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
GridView.aspx.cs
protected void gvCurrentQueueStatus_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Button controlButton = (Button)e.Row.FindControl("btnSubmitCommand"); // FindControl fails
HiddenField hdnQueueNumber = (HiddenField)e.Row.FindControl("hdnQueueNumber"); // FindControl succeeds
// Other stuff
} // breakpoint here, successfully finds htnQueueNumber, but not btnSubmitCommand
}
Try this:
Button controlButton = e.Row.FindControl("btnSubmitCommand") as Button ;
Or try this way:
foreach (GridViewRow row in gvCurrentQueueStatus.Rows) {
Button controlButton = (Button)gvCurrentQueueStatus.Rows[row.RowIndex].FindControl("btnSubmitCommand");
}
So apparently my button was in fact getting properly assigned to, it was just not reporting as such when mousing over the variable in my debugger.
I'm not actually crazy, Visual Studio's debugger is however and needed the old "have you tried turning it off and on again".
Sorry about that those that helped look into my issue :3
I have a TextBox in a GridView which takes a user input. The problem is that it loses its value when I click on a Button placed in the GridView footer which adds an empty row to the GridView,How can I fix it?
Also, this happens only when I set its Disabled attribute to false in RowDataBound event. But if I set the ReadOnly attribute, everything works fine.
This is the TextBox in GridView.Bound to a DB data:
<asp:TemplateField HeaderText="Shift Code">
<ItemTemplate>
<asp:TextBox runat="server" CssClass="txtingrid" ID="lblShiftCode" Text='<%# Eval("SHIFTCODE") %>'></asp:TextBox>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" Width="50px" />
</asp:TemplateField>
this is what I do in the RowDataBound:
if (e.Row.RowType == DataControlRowType.DataRow)
{
lblShiftCode.Attributes.Add("disabled", "true");
}
Please help.:)
lblShiftCode.Attributes.Add("disabled", "disabled");
If you are doing a post back on the button click, you will have to set the UseSubmitBehavior attribute to "false" on the button to retain values of the disabled controls.
More info here
Its very simple just rebind the gridview after clicking the button. Suppose you are using gridbind(); method to bind your gridview.So you have to call this method again after button click.
This a pretty simple question, I'm just not sure how to do it exactly. I would like to bind a Button or perhaps ImageButton to a GridView in ASP.NET/C#. Currently, the GridView has two columns and is bound to a DataTable with two columns. I want to add a third column to the GridView, which will include the Button.
I know GridView has ButtonField, but I'm not too sure how to go about using it to do what I want. I want to dynamically generate these Buttons and add them to the GridView.
Here is how my GridView looks right now:
<asp:GridView
ID="GridView1"
Runat="server">
<Columns>
<asp:HyperLinkField
HeaderText="Display Name"
DataNavigateUrlFields="DISPNAME"
DataNavigateUrlFormatString="ViewItem.aspx"
DataTextField="DISPNAME">
<ItemStyle Width="70%" />
</asp:HyperLinkField>
<asp:BoundField
DataField="TypeDisp"
HeaderText="Type">
<ItemStyle Width="20%" />
</asp:BoundField>
</Columns>
</asp:GridView>
You can use a template field like the following,
<TemplateField>
<ItemTemplate>
<asp:ImageButton ImageUrl="image url" CommandName="SomeCommand" CommandArgument='<%# Eval("Id") %>'/>
</ItemTemplate>
</TemplateField>
Then you can handle the RowCommand event of the GridView and check the e.CommandName to see what command to be executed and you can get the e.CommandArgument as well which could be the row Id like I used in the code above.
If we are talking a button that's always present, you can use ButtonField, or even use a TemplateField and provide the template with the button, and bind the data to the button (sounds like you may want to bind data to the attributes of the button?)
If you are looking to dynamically generate buttons in the UI, tap into the RowCreated event and add the button the GridView. You'd have to do this on every page load; the GridView won't remember a button created programmatically.
HTH.
I've been working for a long time with GridViews and DetailsViews, but yesterday I've come across a new scenario, which I quite do not understand.
I have a GridView with ImageButton (CommandName="Insert") which will change the mode of the DetailsView to Insert. Afterwards I'll look for a DropDownList inside that DetailsView and add some items dynamically. Works fine, but one first the first time I press that ImageButton. If I click on "Cancel" in the DetailsView and press the ImageButton again, the .FindControl() Method returns null. What life cycle problem am I facing here?
I've created this sample: (To make it run in your Visual Studio, just bind a DataSource to the DetailsView, otherwise it will not be rendered)
Markup:
<asp:GridView ID="gvCategory" runat="server" OnRowCommand="gvCategory_RowCommand">
<Columns>
</Columns>
<EmptyDataTemplate>
<asp:ImageButton ImageUrl="~/images/add.png" ID="ibAdd" runat="server" CommandName="Insert" />
</EmptyDataTemplate>
</asp:GridView>
<asp:DetailsView ID="dvCategory" runat="server" Width="150px" AutoGenerateRows="false"
AutoGenerateInsertButton="True" DataSourceID="LinqDataSource1">
<Fields>
<asp:TemplateField HeaderText="foo">
<InsertItemTemplate>
<asp:DropDownList ID="ddlCategory" runat="server" Width="150"></asp:DropDownList>
</InsertItemTemplate>
</asp:TemplateField>
</Fields>
</asp:DetailsView><asp:LinqDataSource ID="LinqDataSource1" runat="server"
ContextTypeName="WebApplication1.DataClasses1DataContext"
TableName="Categories"></asp:LinqDataSource>
Codebehind:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
this.gvCategory.DataBind();
}
}
protected void gvCategory_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Insert")
{
this.dvCategory.ChangeMode(DetailsViewMode.Insert);
DropDownList _ddlCat = (DropDownList)this.dvCategory.FindControl("ddlCategory");
if (_ddlCat != null)
{
_ddlCat.Items.Clear();
_ddlCat.Items.Add(new ListItem() { Text = "-- empty --", Value = "-1" });
}
}
}
I have also tried using a ItemTemplate, and not a InsertItemTemplate, but this results in the same. After using the ChangeMode-Method the DetailsView.CurrentMode == InsertMode. The only thing I can think of is, that the markup is already generated for the ItemTemplate and changing the Mode to InsertMode can't affect the rendered markup, or something like this.
Does anybody have a solution to this? =)
I think you are on the right track. It's hard to tell without seeing all of the code, but basically any time you change the rendering mode of a row in a repeater-type control you need to rebind it so that it's re-rendered. The fact that FindControl is returning NULL means only one thing: THE CONTROL IS NOT THERE. Which means it was not rendered. You can verify this by looking at the control hierarchy.
So, in your handler for Cancel are you rebinding?