Reputation: 1794
I have used gridview in my aspx page.. In that i have a five radio buttons in a single cell aligned horizontally.
<asp:GridView ID="CrowdRatingGrid" runat="server" AutoGenerateColumns="false" AllowPaging="true" PageSize="4" OnPageIndexChanging="CrowdRatingGrid_PageIndexChanging" ViewStateMode="Enabled">
<PagerSettings Mode="Numeric" PageButtonCount="4" />
<Columns>
<asp:BoundField DataField="idea_id" HeaderText="Idea ID"></asp:BoundField>
<asp:TemplateField>
<HeaderTemplate>
Rating<br />1 2 3 4 5
</HeaderTemplate>
<ItemTemplate>
<asp:RadioButton runat="server" GroupName="rating" ID="1" />
<asp:RadioButton runat="server" GroupName="rating" ID="2" />
<asp:RadioButton runat="server" GroupName="rating" ID="3" />
<asp:RadioButton runat="server" GroupName="rating" ID="4" />
<asp:RadioButton runat="server" GroupName="rating" ID="5" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
This is my database table structure:
idea_id rating_id
23882 3
23883 5
63720 1
This is my codebehind code for binding the gridview data:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindCrowdRatingGrid();
}
}
private void BindCrowdRatingGrid()
{
Campaigns campaign = new Campaigns();
ObjectResult<GetCrowdRating_Result> resultList = campaign.GetCrowdRatingIdeas();
CrowdRatingGrid.DataSource = resultList;
CrowdRatingGrid.DataBind();
}
I am getting the "idea_id" value displayed correctly in the grid under header "Idea ID" Also i need the radiobutton to be checked based on the value of rating_id.. If rating_id is 3 i need the radiobutton under 3 to be checked if 1 the radiobutton under 1 to be cheked..
How to achieve this?
Upvotes: 1
Views: 10253
Reputation: 22448
Try to put RadioButtonList into ItemTemplate instead:
<asp:RadioButtonList runat="server' ID="Rating"
SelectedValue='<%# Bind("rating_id") %>' RepeatDirection="Horizontal" >
<asp:ListItem Value="1" />
<asp:ListItem Value="2" />
<asp:ListItem Value="3" />
<asp:ListItem Value="4" />
<asp:ListItem Value="5" />
</asp:RadioButtonList>
To remove text from each radiobutton set empty Text
proprty for each ListItem. For hiding ListItem
with 0 value you can set RepeatLayout="Flow"
on RadioButtonList and set some CssClass property value like this: CssClass="rating"
. Then add onto a page this style rule:
.rating > input:first-child
{
display: none;
}
Also as an another option available you can use SelectedIndex
property of the RadioButtonList instead of SelectedValue and decrement each rating_id
in resultList before binding to 1. This way you don't need surrogate ListItem with 0 value.
Upvotes: 3
Reputation: 66
Try using a RadioButtonList.
<asp:RadioButtonList id="myList" runat="Server">
<asp:ListItem id="1" value="1">1</asp:ListItem>
<asp:ListItem id="2" value="2">2</asp:ListItem>
<asp:ListItem id="3" value="3">3</asp:ListItem>
<asp:ListItem id="4" value="4">4</asp:ListItem>
<asp:ListItem id="5" value="5">5</asp:ListItem>
</asp:RadioButtonList>
And then using something like this from your code behind:
myList.Items.FindByValue(yourDBValue).Selected = true;
Edit:
Or, bind the selectedValue in the control itself; as Yuriy suggested.
Upvotes: 2
Reputation: 102
maybe you can try to do a .FindControl by a string name, that is, if you are looking for your radio button inside the gridView. Just bind the grid view to your data source and then on the rowDataBound search for your radio button on every row. Lets say you have the 5 radio buttons with names: rb1, rb2, rb3 and so on.. so when you get the rating id just add it to the string name like so
protected void gridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{
GridViewRow row = (GridViewRow)e.item;
..YourObject.. data = (..YourObject..)e.item.DataItem;
//and now you search for your radio button
RadioButton button = (RadioButton)row.FindControl("rb" + data.rating_id);
//or just use the id as a name like you have allready named them
button.cheched = true;
}
Upvotes: 0
Reputation: 4089
GridView control has RowCreated
event. There you can make the radio button checked based on the value from the database. You would probably have to use FindControl()
to get the correct radio button.
This page has a good example of how to create OnRowCreated
event handler:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowcreated.aspx
Upvotes: 0