Creating dynamic RadiobuttonList - asp.net

What im trying to accomplish is I have a table which im creating dynamically and I want in the first tablecell a radiobutton, second tablecell first name, third tablecell last name. The second and third tablecell work fine but having trouble with the radiobutton. Im making it a radiobutton list b/c only one name should be selectable. Below is how im creating the radiobutton list. I have omitted the second and third tablecell as its working properly. The yesNo variable is used to say is this the first row in the table. Any help would be very much appreciated.
tblrow = New TableHeaderRow
tblcell = New TableCell
If yesNo = "yes" Then
radList = New RadioButtonList
radList.ID = newVar & "_list"
Else
Dim item As New ListItem
radList.Items.Add(item)
End If
tableName.Rows.Add(tblrow)

Looks like you are creating a new RadioButtonList with one button in it for every row.
Try using instead the HtmlInputRadioButton class. Here is a nice description of it and it's use.
Here's the relevant bit:
HtmlInputRadioButton controls can be grouped together by specifying a common value for the Name property of each radio button you want to include in the group.
Note When you group HtmlInputRadioButton controls together, only one radio button in the group can be selected at a time.

Related

how to dynamically add rows to a GridView through a DataTable without affecting any table?

I have to show a gridview in my page which doesn't concerns any table to be bound with it. Still it should store one Tax detail per row, as entered by user. Following is the image which will clarify what problem I'm facing.
The gridview shown above should not show any records at first except the footer template of two textboxes and an Add New Record Button, when you are upto add any record.
Once clicked on Add New Record button it should show a row without action column.
(I'm having quite a hard time here)Once clicked on Add/Insert Button(separate from the gridview) store the entered record somehow.
Then I have to show the record in Another concrete GridView with Edit Button.
Once I click on edit button, control should return to my dynamic gridview page. Here, my dynamic gridview will show previously entered record with action column containing a delete button.
Can you Help Me? I don't want help with the code. Just point me in the right direction(s), please.
You should try this.
private DataTable AddEmptyRow()
{
DataTable originalDataTable = GetItems();
DataTable newDataTable = originalDataTable.Clone();
string category = originalDataTable.Rows[0][2].ToString();
foreach (DataRow dRow in originalDataTable.Rows)
{
if (category != dRow[2].ToString())
{
DataRow newDataRow = newDataTable.NewRow();
newDataTable.Rows.Add(newDataRow);
category = dRow[2].ToString();
}
newDataTable.ImportRow(dRow);
}
return newDataTable;
}
You can add row by using DataRow newDataRow = newDataTable.NewRow();.

Insert separator row in Gridview

I'm new to ASP.NET VB, and i'm having trouble and dont really understand the code of putting in row seperators in a gridview. I have 5 columns and the fifth columns rows field text is what i would like inserted into the new seperator row. There may be more than one per question. To populate my gridview i'm calling upon a stored procedure "Select_Problems" and carrying a persons name from another page through "SvarRecord" and matching it up with the SP #Name. The fifth column in the SP has four possibilities "1. Person upset?", "2. Is Person is resisting?", and so on. My code is as follows and i would like to have an easy way to do this but to make it look nice along with a color to divide each question. Thank you
Public Sub LoadGrid2()
Dim MyConn As New SqlConnection((ConfigurationManager.ConnectionStrings("Customer_Service").ConnectionString))
Dim MyCmd As New SqlDataAdapter("Select_Problems", MyConn)
MyCmd.SelectCommand.CommandType = CommandType.StoredProcedure
MyCmd.SelectCommand.Parameters.Add(New SqlParameter("#Name", SqlDbType.VarChar))
MyCmd.SelectCommand.Parameters("#Name").Value = Session("SvarRecord")
Dim ds As New DataSet
MyConn.Open()
MyCmd.Fill(ds, "Overview")
GridView2.DataSource = ds
GridView2.DataBind()
End Sub
I know your supposed to do this in the Rowdatabound of the Gridview but I dont understand the code behind it to do this which would be great to do.
In the RowDataBound event, you could check the index of the row currently being bound and determine if it is the last row in a set of 5 and then apply styling to the row to give a visual cue of separation without actually generating a blank separator row, like this:
Protected Sub GridView2_RowDataBound(sender As Object, e As GridViewRowEventArgs)
' Add a visual separator via CSS border property to every 5th row
If e.Row.RowIndex Mod 5 = 0 Then
' Apply style to each cell in the row
For Each cell As TableCell In e.Row.Cells
cell.Attributes.Add("Style", "border-bottom: red 5px solid")
Next
End If
End Sub
Note: My math might be off by one so you may need to change the styling to the top of the row (border-top instead of border-bottom), but the idea is the same.

Dynamically Adding Multiple User Controls vb.net

I have a User Control which returns a table of data, which in some cases needs to be looped, displaying one on top of another.
I am able to dynamically add a single instance of this by placing a fixed placeholder in the page.
I am now trying to work out how to add more than one, given that I don't know how many might be needed I don't want to hard code the Placeholders.
I've tried the following, but I am just getting one instance, presumably the first is being overwritten by the second
My HTML
<div id="showHere" runt="server"/>
VB
Dim thisPh As New PlaceHolder
thisPh.Controls.Add(showTable)
showHere.Controls.Add(thisPh)
Dim anotherPh As New PlaceHolder
anotherPh .Controls.Add(showTable)
showHere.Controls.Add(anotherPh)
How do I make it add repeated tables within the showHere div?
I would advise generating a different ID for each of your table. For example,
Dim i As Integer
i = 0
For each tbl in ShowTables
tbl.ID = "MyTab" + i.ToString()
i = i + 1
showHere.Controls.Add(tbl)
showHere.Controls.Add(New LiteralControl("<br />"))
Next
On other hand, it would make more sense to have a your user/custom control generate html for a single table and then nest your user/custom control within a repeater (or similar control such as ListView etc).
Did you tried simply, this:
For each tbl in ShowTables
showHere.Controls.Add(tbl)
showHere.Controls.Add(New LiteralControl("<br />"))
Next
After fussing with this issue myself, I stumbled across below solution.
On button click()
LocationDiv.Visible = True
Dim existingItems As New List(Of Object)
If Not Session("existingItems") Is Nothing Then
existingItems = CType(Session("existingItems"), List(Of Object))
For Each item As Object In existingItems
LocationDiv.Controls.Add(item)
Next
existingItems.Clear()
End If
LocationDiv.Controls.Add(New LiteralControl("<b>" & Text & "</b>"))
For Each item As Object In LocationDiv.Controls
existingItems.Add(item)
Next
Session.Add("existingItems", existingItems)

How to make an option in the DropDown as first option only when list opens?

I have a DropDownList that is filled with values returned by a SQL Query. After the list is filled, I add two options manually to this DropDown: -- Select -- and ALL.
When the page loads, the DropDownList shows -- Select -- as the default value. When the user opens the DropDownList, the SQL Query returned list opens along with ALL option also.
I want to make this ALL appear as the fist item in the list when the list opens. I can't used: DropDownList.SelectedIndex because I want have already made -- Select -- as the selected index.
How to make ALL the first option when the list opens?
I guess one solution would be to use a textbox / dropdownlist combined. And have the select text as default in the textbox. And the first option in dropdownlist would be ALL. Then disable the editing of the textbox to make it appear as a regular dropdownlist.
Like this: Textbox and dropdownlist combined
Hope this helps.
As much as I understand your dream is to have a dropdownlist like this
SELECT(this one comes as selected)
ALL(the first item in the dropdownlist)
V1(from your query)
V2(from your query)
V3(from your query)
..
.
You can do this
string selectStr = "SELECT";
string allStr = "ALL"
ListItem allLI = new ListItem(allStr,allStr);
ListItem selectLI = new ListItem(selectStr,selectStr);
DropDownList.Items.Add(selectLI);
DropDownList.Items.Add(allLI);
//code to fill the DropDownList with the list that your query returns
DropDownList.SelectedValue = selectStr;

Adding hyperlinks dynamically by code

I have a web form but I have to do this by code since I dont know the number of hyperlinks I need from the beginning.
How can I add some hyperlinks with Image in a label, the number of hyperlink depends on the number of rows of a query, and each row give me the link information to navigate.
Thanks in advance.
As you loop through your data you can manually add a link to the row something like this:
For i As Integer = 0 To 10
Dim row As New HtmlTableRow
row.Cells.Add(New HtmlTableCell)
Dim Link As New HyperLink
Link.Text = "WhateverText"
Link.NavigateUrl = "page.aspx"
Link.ImageUrl = "~/Theme/Images/SomeImage.gif"
Link.ToolTip = "ToolTipText"
row.Cells(0).Controls.Add(Link)
Next
That of course adds the link as the first cell in an html table. Not sure how you plan to display your data.
In response to the comment below. You can instead insert the new cell something like this
For i As Integer = 0 To 10
Dim row As New HtmlTableRow
Dim cell As New HtmlTableCell
row.Cells.Insert(1, cell)
Dim Link As New HyperLink
Link.Text = "WhateverText"
Link.NavigateUrl = "page.aspx"
Link.ImageUrl = "~/Theme/Images/SomeImage.gif"
Link.ToolTip = "ToolTipText"
row.Cells(0).Controls.Add(Link)
Next
You could also simply add the control to the existing cell that the label is in instead of making a new cell. You can do that by the index value of your existing cell (starting at 0 for each cell that is in the row)
This question is similar to what you want to do:
Auto increment asp control ID
Two options either use a repeater or dynamically add the controls to a panel or some other container control.

Resources