Radio Buttons in Gridview - c#

I have a list of games. Each game has a visiting team and a home team. I would like to allow the user to choose either the visiting team or the home team. So I converted to template fields and put radio buttons in. But clearly that isn't right. I need a radio button list, I think. Because with just radio buttons, they can select both. is this possible with a gridview? most tutorials I see only have one radio button in the row, so this isn't the issue. here is my gridview:
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" DataKeyNames="id_game" DataSourceID="sqlPopulateGames">
<Columns>
<asp:BoundField DataField="id_game" HeaderText="id_game" InsertVisible="False" ReadOnly="True" SortExpression="id_game" />
<asp:TemplateField HeaderText="visitor" SortExpression="visitor">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("visitor") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<br />
<asp:RadioButton ID="RadioButton1" runat="server" Text='<%# Eval("visitor") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="home" SortExpression="home">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("home") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:RadioButton ID="RadioButton2" runat="server" Text='<%# Eval("home") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="game_time" HeaderText="game_time" SortExpression="game_time" />
</Columns>
</asp:GridView>

because with just radio buttons, they can select both.
Not if those radio buttons are grouped in the markup. Radio buttons are designed to be mutually-exclusive, but they need to know which other radio buttons to be exclusive with. The GroupName property in ASP.NET is used to set this.
Something like this:
<asp:RadioButton GroupName="SomeGroup" ID="RadioButton1" runat="server" Text='<%# Eval("visitor") %>' />
With that set, this would be mutually exclusive for any other RadioButton with the GroupName set to "SomeGroup".
Since this is happening inside a repeated control, you probably want a separate group for each iteration. So you can bind it to some value in the backing data:
<asp:RadioButton GroupName='<%# Eval("someValue") %>' ID="RadioButton1" runat="server" Text='<%# Eval("visitor") %>' />
If someValue is unique for each record in the result, then you would end up with a grouped pair of mutually exclusive radio buttons for each row.

Related

How would I dynamically bind a grid view with 3 drop down lists in c# asp.net?

net gridview and I do not wish to use the built in controls and want to bind the data manually in c#. Could some one let me know where to start and how can I use 3 different drop downs to add more filtering?
asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" OnRowDataBound="gvContactorRowDataBound" Gridlines="Vertical" >
<Columns >
<asp:TemplateField HeaderText="Full Name" SortExpression="contactname" HeaderStyle-BackColor="deepskyblue">
<EditItemTemplate>
<asp:TextBox ID="txtcontactname2" runat="server" Text='<%# Bind("contactname") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidatorcontactname2" runat="server" ErrorMessage="Full Name is required for contractor update!" Text="*" ForeColor="Red" ControlToValidate="txtcontactname2" display="none"></asp:RequiredFieldValidator>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblcontactname2" runat="server" Text='<%# Bind("contactname") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="phone" HeaderText="Phone" SortExpression="phone" HeaderStyle-BackColor="deepskyblue"/>
<asp:BoundField DataField="email" HeaderText="Email" SortExpression="email" HeaderStyle-BackColor="deepskyblue"/>
You did not mentioned your scenario, therefore i assume that you want show all of students in grid view and in each row have a Drop down to select teacher. You have another data base Teachers and each teacher has Id and Name. So in your code behind you gathered all of the teachers:
protected List<teacher> teachers;
In each row of Students gridview you must have this column:
<asp:TemplateField HeaderText="Teacher">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" CssClass="selectClip" DataSource="<%# teachers %>"
DataTextField="Name" DataValueField="Id" AppendDataBoundItems="true">
<asp:ListItem Text="<---Select Teacher--->"></asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblShowTeacher" runat="server" Text='<%# Bind("Teacher") %>' />
</ItemTemplate>
</asp:TemplateField>
If you want to have your teachers in a SqlDataSource, change this:
DataSource="<%# teachers %> to this: DataSourceId="teachersDataSourceId"

Set Item limit to be display in ajaxToolkit ComboBox or set the Combobox position downwards in asp.net

I have a ajaxtoolkit combobox inside a gridview and it works perfectly, but the problem is when I click the dropdown button the list overlaps the page so I want to limit the number to be displayed to 10 items per scroll. Next problem is when I try to add an update panel to the page, It destroys the rendering of the combobox. How can I solve this? Thanks in advance
here is my code for the combobox inside the gridview
<asp:GridView CssClass="pad" ID="dgvOrder" runat="server" BorderStyle="Double" AutoGenerateColumns="False" OnRowDataBound="dgvOrder_RowDataBound" OnRowDeleting="dgvOrder_RowDeleting" HorizontalAlign="Center" >
<Columns>
<asp:TemplateField HeaderText="No." AccessibleHeaderText="No.">
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="Description" AccessibleHeaderText="Description">
<ItemTemplate >
<ajaxToolkit:ComboBox ID="ddlItems" AutoPostBack="True" AppendDataBoundItems="True" onmouseover="this.size=4;" onmouseout="this.size=1;" DataTextField="item_desc" DataValueField="item_id" runat="server" AutoCompleteMode="Suggest" ItemInsertLocation="Prepend" OnSelectedIndexChanged="ddlItems_SelectedIndexChanged" DropDownStyle="DropDown" BorderStyle="Double" ></ajaxToolkit:ComboBox> <br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="Please pick an item" ControlToValidate="ddlItems"></asp:RequiredFieldValidator>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField AccessibleHeaderText="Inventory Code" HeaderText="Inventory Code">
<ItemTemplate>
<asp:Label ID="lblInvCode" runat="server" Text=""></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField AccessibleHeaderText="Qty" HeaderText="Qty">
<ItemTemplate>
<asp:TextBox ID="txtQty" runat="server" OnTextChanged="txtQty_TextChanged" AutoPostBack="True" onkeyup ="ValidateText(this);"></asp:TextBox><br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator23" runat="server" ErrorMessage="Please input qty" ControlToValidate="txtQty"></asp:RequiredFieldValidator>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField AccessibleHeaderText="UOM" HeaderText="UOM">
<ItemTemplate>
<asp:Label ID="lblUOM" runat="server" Text=""></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField AccessibleHeaderText="SOQ" HeaderText="SOQ">
<ItemTemplate>
<asp:Label ID="lblSOQ" runat="server" Text=""></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField AccessibleHeaderText="Reason" HeaderText="Reason">
<ItemTemplate>
<asp:TextBox ID="txtReason" runat="server" Enabled="False"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowDeleteButton="True" />
</Columns>
</asp:GridView>
and here is my code for the updatepanel
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
</ContentTemplate>
<Triggers>
(whole page inside)
<asp:AsyncPostBackTrigger ControlID="btnSearch" EventName="Click" />
<asp:AsyncPostBackTrigger ControlID="txtIdNo" EventName="TextChanged" />
</Triggers>
</asp:UpdatePanel>
Long Item Lists / Multiple Input Controls
All of the items in a ComboBox's list will be rendered to the web page it exists in. The AutoCompleteExtender, on the other hand, retrieves items from its ServiceMethod after the page is rendered. When your ComboBox contains a rather long list of items, or when you have a relatively large number of ComboBoxes on the same page (or within the same UpdatePanel), load times could be slowed down significantly. When ComboBoxes perform slowly because of the amount of markup they must render to the browser, an AutoCompleteExtender can be used instead to increase performance.
<ajaxToolkit:ComboBox ID="ComboBox1" runat="server"
DropDownStyle="DropDown"
AutoCompleteMode="None"
CaseSensitive="false"
RenderMode="Inline"
ItemInsertLocation="Append"
ListItemHoverCssClass="ComboBoxListItemHover"
<asp:ListItem>...</asp:ListIem>
...
For more information your can look it in this link.

Selected rows using check box should be displayed inside the footer row

I have requirement when we select the rows of the gridview using checkbox those selected rows should be displayed in the footer row of the grid
please let me know how we can do it using asp.net and c# programming.
<asp:GridView runat="server" ID="gv" AutoGenerateColumns="False" CellPadding="4">
<Columns>
<asp:TemplateField HeaderText="Username">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Username") %>'></asp:Label>
<asp:HiddenField ID="hdn" runat="server" Value='<%#Eval("Id") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Username") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="ftrtxtunme" runat="server" Text='<%#Eval("Username") %>'></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
You will have to perform a sort of the whole grid on the specific rows. I'm not sure wheter the Checked state will make it on top or bottom but if it does not work simply change Descending to Ascending.
First select the event in which you want the sorting to be called. Let's imply it's on some button click here.
myButton.Click += (s, e) =>
{
myDataGridView.Sort(dataGridViewCheckBoxColumn, ListSortDirection.Descending);
};
You might want to check the SortMode Property before sorting the grid.

Javascript Breaking on < & > in a GridView edit box

I've got an Asp.Net GridView inside an UpdatePanel. It all works fine, except when one of the columns includes HTML special characters like < and >. The GridView is bound to a List<Entity> and the Entity class has a property Regex which is a System.Text.RegularExpressions.Regex.
At first I had this:
<asp:TemplateField HeaderText="RegEx">
<ItemTemplate>
<asp:Label ID="RegExLabel" runat="server" Text='<%#Eval("Regex") %>'
ToolTip='<%#Eval("Regex") %>' Width="102px" CssClass="Wrap" />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="RegExTextBox" runat="server" Text='<%#Eval("Regex") %>'
Width="98px" />
</EditItemTemplate>
</asp:TemplateField>
With a value of (?<capture>\d+) this displayed ?\d+ when not editing, and when editing this row I got a script error and the edit, update and cancel buttons no longer work.
Then I tried the answer in this question and had this:
<asp:TemplateField HeaderText="RegEx">
<ItemTemplate>
<asp:Label ID="RegExLabel" runat="server"
Text='<%#System.Web.HttpUtility.HtmlEncode(Eval("Regex").ToString()) %>'
ToolTip='<%#System.Web.HttpUtility.HtmlEncode(Eval("Regex").ToString()) %>'
Width="102px" CssClass="Wrap" />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="RegExTextBox" runat="server"
Text='<%#System.Web.HttpUtility.HtmlEncode(Eval("Regex").ToString()) %>'
Width="98px" />
</EditItemTemplate>
</asp:TemplateField>
This is slightly better, in that the tooltip and the non-editing version display correctly, but when I start editing I see: (?<capture>\d+) with the HTML Entities displayed raw. Does anyone know a way to encode the values (to stop the script error) while still displaying them correctly without the HTML entities in their raw state when editing?
<asp:TemplateField HeaderText="RegEx">
<ItemTemplate>
<asp:Literal Mode="Encode" ID="RegExLabel" runat="server" Text='<%#Eval("Regex") %>'
ToolTip='<%#Eval("Regex") %>' Width="102px" CssClass="Wrap" />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="RegExTextBox" runat="server" Text='<%#Eval("Regex") %>'
Width="98px" />
</EditItemTemplate>
</asp:TemplateField>
and in the page directive add this ValidateRequest="false"

Need idea to design my grid. asp.net(c#) gridview

My DataGrid contain totally 32 columns.I need to display my first 4 column as default column.
For the remaining Column i need to view using scroll bar.(not Page scroll need scroll bar inside my grid view)
When I've had to do this, what I did was create 2 Gridviews next to each other
The first Gridview should have only the freezable columns. The second will have the rest, and will be in a scrollable div tag.
I had defined amount of of rows, so it wasn't a problem for me, but if you scroll down on one grid, the rows won't match the second grid.
Here's another solution using CSS..but I haven't tried it:
http://www.codeproject.com/KB/webforms/FreezePaneDatagrid.aspx
If you use third party controls like the Telerik RadGrid, they usually have built in properties to control Freezing Columns.
Try this
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:BoundField HeaderText="Column 1" />
<asp:BoundField HeaderText="Column 2" />
<asp:BoundField HeaderText="Column 3" />
<asp:BoundField HeaderText="Column 4" />
<asp:TemplateField>
<ItemTemplate>
<div style="overflow: scroll; width: 400px;">
<asp:Label ID="Label1" runat="server" Text='<%# Eval("Column 5") %>'></asp:Label>
<asp:Label ID="Label3" runat="server" Text='<%# Eval("Column 6") %>'></asp:Label>
<asp:Label ID="Label4" runat="server" Text='<%# Eval("Column 7") %>'></asp:Label>
<asp:Label ID="Label5" runat="server" Text='<%# Eval("Column 8") %>'></asp:Label>
<asp:Label ID="Label6" runat="server" Text='<%# Eval("Column 9") %>'></asp:Label>
....
<asp:Label ID="Label7" runat="server" Text='<%# Eval("Column 32") %>'></asp:Label>
</div>
</ItemTemplate>
</asp:TemplateField>
</Columns>

Categories

Resources