Gridview with radio column, radio id problem - c#

I have a gridview, and I put a radio column for it. But radio ids are not unique.
I've read
http://www.asp.net/Learn/data-access/tutorial-51-cs.aspx
http://shawpnendu.blogspot.com/2009/02/problem-to-group-radio-button-across.html
https://web.archive.org/web/20210510012500/http://aspnet.4guysfromrolla.com/articles/122602-1.aspx
but they are complicated.
Do you have other solution, with c# code behind? I've also tried to give unique id with a function from code behind, but ID='<%# function(...) %>' is not allowed.
Thank you in advance
there is gridview code:
<asp:GridView ID="gridView_stLists"
runat="server"
AutoGenerateColumns="False"
CellPadding="3"
BorderStyle="NotSet"
CssClass="table_layout"
Width="500">
<RowStyle CssClass="table_body" />
<Columns>
<asp:TemplateField HeaderStyle-Width="20">
<ItemTemplate>
<asp:RadioButton ID="rdBtn_stdl" runat="server"
OnCheckedChanged="rdBtn_stdl_CheckedChanged"
AutoPostBack="True"
GroupName="stdl" value='<%# Eval("uri") %>' />
</ItemTemplate>
I think radios must have different IDs in ID="rdBtn_stdl" section

Not totally sure what your question is, but here goes.
If you need to find a RadioButton in a GridViewRow, first get the row, then use FindControl()
GridViewRow myRow = GridView1.Rows[index];
RadioButton myRadioButton = (RadioButton)myRow.FindControl("nameOfRadioButton")
You can do that in reverse (if you need to handle the event RadioButton1_CheckedChanged, for example) by first casting the object sender to RadioButton, then casting RadioButton.Parent.Parent to GridViewRow, from which you can get the index.
Question:
Is the column you added a Template Column, in which you places a RadioButton? My two solutions will only work in this case.

The radio buttons need to have the same name attribute. Not unique. This is how the browser knows how to group them.

Found a good method at:
http://www.asp.net/Learn/data-access/tutorial-51-cs.aspx

Related

Values GridView in cells

I am trying to get values of string that I keep inside GridView.
When I use regular BoundFields, things work get. I get whatever is needed with:
string my_value = myGrid.Rows [rowIndex].Cells[1].Text;
However, one grid needs to have hyperlinked entries in one of the columns. I did:
<asp:BoundField DataField="domainName"
HeaderText="Domain"
SortExpression="domainName"
HtmlEncode="false"
DataFormatString="<a href=DomainConfiguration.aspx?suffix={0}>{0}</a>"
My ASPX page shows the correctly formed hyperlinks. However, if I retrieve Text for the cell, it returns "<\a href=DomainConfiguration.aspx?suffix=example.com>example.com</a>" [without the two extra slashes], instead of "example.com"
What do I need to do to get GridView working the way I want? [Yes, I would rather use GridView and not another control.]
Thank you.
Instead of asp:BoundField to show hype link it is better use asp:HyperLinkField like:
<Columns>
<asp:HyperLinkField DataNavigateUrlFields="domainName" HeaderText="domainName" DataNavigateUrlFormatString="DomainConfiguration.aspx?suffix={0}" DataTextField="domainName" />
</Columns>
and to get string value of that column you should try this:
string my_value= ((HyperLink)myGrid.Rows[rowIndex].Cells[1].Controls[0]).Text;
instead your asp:Bounfield use
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink ID="editLink" runat="server" onclick='<%#Eval("EditLink") %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>

Binding a Button to a GridView

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.

Need to make a specific column editable upon clicking edit

I have a gridview with "Edit Update Cancel" command field.
When I click Edit, all the columns in the particular row becomes editable.
I just need to have 2 specific columns editable. How is that made possible ?
(Screen Shot Attached)
[In the screen shot all 3 columns are editable, I just need the second and third to be editable]
Thanks in Advance.
Just set the other columsn to read-only:
<asp:BoundField DataField="Id" HeaderText="Id" ReadOnly="True" />
It is very easy. The column you want not editable then place just label and bind properly.
<EditItemTemplate>
<asp:Label Runat="server" Text='<%# Bind("UnitPrice", "{0:c}") %>' ID="Label1"></asp:Label>
</EditItemTemplate>
Here I use label in my EditItemTemplate because when the user clicks on edit button, the textbox will not come up. Rather, the value for that record will show through label, and as a result the user can not update that field.

Put multiple rows of a gridview into edit mode

I have the need to allow a user to "tab through" making edits on a gridview. There will be one editable column in the row data. The user should be able to hit tab and go to the next row to edit said column.
I have not found any easy method to accomplish this task. I found a way to programmatically put a gridview into edit mode, but in testing the code below it works for only 1 row at a time.
reviewTransferGV.EditIndex = 0;
reviewTransferGV.Rows[0].RowState = DataControlRowState.Edit;
reviewTransferGV.EditIndex = 1;
reviewTransferGV.Rows[1].RowState = DataControlRowState.Edit;
reviewTransferGV.DataBind();
I did a workaround by creating a property in the page:
protected bool IsEditMode
{
get { return this.EditMode; }
set { this.EditMode = value; }
}
Then in the GridView I have the controls for view and edit mode inside an item template. Setting the visibility based on the property value:
<asp:TemplateField SortExpression="Status" HeaderText="Status">
<ItemTemplate>
<asp:Label Id="lblStatus" Text='<%# Eval("Status") %>' Visible='<%# !IsEditMode %>' runat="server" />
<asp:TextBox ID="txtStatus" Text='<%# Eval("Status") %>' Visible='<%# IsEditMode %>' runat="server" />
</ItemTemplate>
This works for editing the whole gridview. You'll probably need to make a few modifications to make it work for individual rows.
I don't believe it is possible for a GridView to have multiple rows in edit mode simultaneously. If you want to edit multiple rows, you will need to roll your own mechanism to do so.
Another point is how to save the results to the DataBase.
While in regular use, we simple call the update command who does the work, in ItemTemplate there are now update button.
So i add a button outside the GridView and in the handler i call the UpdateRow method manually for each row.

Gridview Column Removing

I have a web application that I am working on(ASP.NET 2.0 C#). In it I have a GridView whose data source is an Oracle database. I get the data into the gridview in my codebehind, and don't set the datasource directly.
I wanted to create a hyperlink field (NAME) that takes me to a details page about a specific record. What ends up happening is that it creates the Hyperlink field as well as the regular field that it gets from the datasource, which I don't want. If I remove the field from my SELECT statement, it gives an error saying something like: "NAME" not found in datasource.
How can I eliminate the regular field, and get a hyperlink field instead? I have tried Gridview.Columns.Remove(columnlocation), but that won't work coz the columns don't exist there originally.
Please Help
Thank you.
Instead of removing columns, disable AutoGenerateColumns property of the gridview and set your hyperlink column manually like that :
<asp:gridview id="GridView1"
autogeneratecolumns="false"
runat="server">
<asp:HyperLinkField DataNavigateUrlFields="UserID"
DataNavigateUrlFormatString="UserDetails.aspx?id={0}"
DataTextField="UserName" />
</asp:gridview>
Sounds like you have AutoGenerateColumns property set to TRUE on your grid. This means that a column is generated for EVERY column you return from your query.
If you want to have some custom columns, you should set AutoGenerateColumns="false" and add all the columns to the GirdView as asp:BoundField and your Hyperlink column as asp:TemplateField
Let me know if I'm off the mark with this
here's some code:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="Name" />
<asp:BoundField DataField="Whatever" />
<asp:TemplateField>
<ItemTemplate>
<a href='<%# Eval("UserId", "URL_TO_USER?userId={0}") %>'>Details</a>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

Categories