make items from checkboxlist links to other pages - c#

I have a checkboxlist.When I check one the value will appear in a table.Now I want that value and each value I check to make it a link.This is my code for getting the checked values:
foreach (ListItem item in check.Items)
{
if (item.Selected)
{
TableRow row = new TableRow();
TableCell celula = new TableCell();
celula.Style.Add("width", "200px");
celula.Style.Add("background-color", "red");
//celula.RowSpan = 2;
celula.Text = item.Value.ToString();
row.Cells.Add(celula);
this.tabel.Rows.Add(row);
Now I want the item.value to make it a link..I'm using c# in asp.net application

Add a Hyperlink control to the celula.Controls collection instead of setting the TableCell's Text property.
// Create a Hyperlink Web server control and add it to the cell.
System.Web.UI.WebControls.HyperLink h = new HyperLink();
h.Text = item.Value;
string url = "~/Default.aspx?Item=" + Server.UrlEncode(item.Value);
h.NavigateUrl = url;
celula.Controls.Add(h);
Note that you need to recreate this table on every postback, so you might want to add this to Page_PreRender instead.

Related

dropdownlist.Text not working

dropdownlist always show the first index of Item populated from database and in debug mode ddlcountry.Text always empty string("").
I have "Philippines" item in my dropdownlist but "Argentina" always shown first in my dropdown instead of "Philippines".
Please help.
//in formload
if(!isPostback)
{
DataTable dtCountry= new DataTable();
dtCountry= network.GetCountry();
for (int row = 0; row < dtCountry.Rows.Count; row++)
{
ddlCoutry.Items.Add(new ListItem { Text = dtCountry.Rows[row][1].ToString(), Value = dtCountry.Rows[row][1].ToString() });
}
}
ddlCountry.Text = "Philippines";
As I mentioned in the comment above, I think your problem is you are trying to select the dropdown option by text but getting confused with .Text property. You can do this:-
ddlCountries.Items.FindByText("Philippines").Selected = true;
Set the selected item to "Philippines" because I assume your list of countries is in alphabetical order.
ddlCountry.SelectedIndex = ddlCountry.Items.IndexOf(ddlCountry.Items.FindByText("Philippines"));
Also I want to point out your variable is misspelled:
**ddlCoutry**.Items.Add(new ListItem { Text = dtCountry.Rows[row][1].ToString(), Value = dtCountry.Rows[row][1].ToString() });

DropDownList lose index after PostBack

I have a dropdown which I fill with data from a SQL server.
I fill the dropdown dynamically in the Page_Init() Event.
Depending on the Value, a ListItem is selected.
Now the problem is, that when I select another Item in the dropdown, that after the postback the selection is reset to the first item in the dropdownlist.
This here is a basic version of my code which does not work:
ArrayList AD_Group_Members = ActiveDirectory.GetMemberOfGroup("AD-Group");
ArrayList ListMachines = SQLQuery.Read("Database", "SELECT idVM, RandomString, Computername, Owner, FROM VM ORDER BY Computername");
for (int i = 0; i < ListMachines.Count; i++)
{
String RandomString = ((Hashtable)ListMachines[i])["RandomString"].ToString();
String Owner = ((Hashtable)ListMachines[i])["Owner"].ToString();
DropDownList DropDownList_Owner = new DropDownList();
DropDownList_Owner.ID = "DropDownList_Owner_" + RandomString;
DropDownList_Owner.Width = Unit.Percentage(95);
DropDownList_Owner.AutoPostBack = true;
DropDownList_Owner.EnableViewState = true;
DropDownList_Owner.SelectedIndexChanged += DropDownList_Owner_SelectedIndexChanged;
Div_Test.Controls.Add(DropDownList_Owner);
for (int y = 0; y < AD_Group_Members.Count; y++)
{
ListItem ListItem = new ListItem();
ListItem.Value = Owner;
ListItem.Text = ((Hashtable)AD_Group_Members[y])["GivenName"].ToString() + " " + ((Hashtable)AD_Group_Members[y])["Surname"].ToString();
if (((Hashtable)AD_Group_Members[y])["Username"].ToString().Equals(Owner))
{
ListItem.Selected = true;
}
DropDownList_Owner.Items.Add(ListItem);
}
}
Where is the issue in my code, that it doesn't work but the example.
Thank in Advance
You have to populate your dropdownlist under this condition on pageload.
Because on every post back your ddl is populated again and loses its selected index.
if (!IsPostBack)
{
//PopulateYourDDL here
}
You filled dropdown in Page_Init() which gets called in every postback and refill your dropdown and hence loses selectedindex.So you have to fill your dropdown inside !ispostback block
if (!IsPostBack)
{
//fill your dropdown here
}
I think the you should have unique values for the dropdown. Also as you have duplicate values in data value field the problem is occurring. It is looking for the first match and selecting it. You could try to fabricate the values which you could uniquely identify. Something like below:
COLUMN_NAME DATA_TYPE
a a_decimal
b b_decimal
c c_decimal
d d_int
e e_int
f f_varchar
g g_varchar
h h_varchar
i i_varchar
j j_varchar
Check out this Useful Source!!!
I hope it helps!!! Have a close look at those comments in the accepted answer section !!!
Also avoid using SelectedIndex_Changed() functions while dealing with Dynamically generated Web controls. Bind the DropdownList under Page_Init() or Page_PreInit(). If you want to perform some functions upon DropDownlist Selection Check out this! to determine the WebControl ID which is triggered and then perform an unique function in the Page_PreInit() or Page_Init().

How to populate DataGridView with textbox and combobox with comboboxcells having different value lists

I have three columns , textbox, combobox and textbox in that order:
this.columnLocalName = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.columnLocalAddress = new System.Windows.Forms.DataGridViewComboBoxColumn();
this.columnLocalPort = new System.Windows.Forms.DataGridViewTextBoxColumn();
And they are in turn in a datagridview like so:
this.dataGridViewLocalProfile.Columns.AddRange(
new System.Windows.Forms.DataGridViewColumn[] {
this.columnLocalName,
this.columnLocalAddress,
this.columnLocalPort});
Later on I will try to add different values to each combobox cell like so:
foreach (profile in localProfile.List)
{
DataGridViewComboBoxCell cell =(DataGridViewComboBoxCell)
(dataGridViewLocalProfile.Rows[dataGridViewLocalProfile.Rows.Count - 1].
Cells["columnLocalAddress"]);
cell.Items.Clear();
cell.Items.Add(profile.Address.ToString());
dataGridViewLocalProfile.Rows.Add(
new string[] { profile.Name, profile.Address, profile.Port });
}
This results in a datagrid with the first column and last column populated and the comboboxcolumn empty. with an dataerror which I handle. The message is:
DataGridViewComboBoxCell value is not valid.
I have read through most of the post, but can not find a solution to this.
I have tried with setting the datasource like so:
cell.DataSource = new string[] { profile.Address };
still getting empty comboboxcolumn with an dataerror saying
DataGridViewComboBoxCell value is not valid.
I think this is extra tricky since I add different values for each comboboxcell.
Can anyone, please help me as to how i can make this work.
/Best
Late to the game, but here's the solution anyways.
The problem is in the foreach loop. The ComboBox cell from the last existing row is populated with an item. But then, a whole new row is added using the current profile object:
dataGridViewLocalProfile.Rows.Add( new string[] { profile.Name, profile.Address, profile.Port });
The items for the ComboBox cell in this new row is empty, therefore profile.Address is not valid. Change the foreach loop to look like this and you're gold:
foreach (Profile p in this.localProfile)
{
DataGridViewRow row = new DataGridViewRow();
row.CreateCells(this.dataGridView1);
DataGridViewComboBoxCell cell = (DataGridViewComboBoxCell)row.Cells[1];
cell.Items.Clear();
cell.Items.Add(p.Address);
row.Cells[0].Value = p.Name;
row.Cells[1].Value = p.Address;
row.Cells[2].Value = p.Port;
this.dataGridView1.Rows.Add(row);
}

dynamic textbox near label in C#

Sorry for reposting this but I haven't figure out how to do it.
string str = string.Empty;
foreach (ListItem item in this.CheckBoxList1.Items)
{
if (item.Selected)
{
str += item.Value + "<br><br>";
TextBox txt1 = new TextBox();
form1.Controls.Add(txt1);
}
}
lbl1.Text = str;
This brings up a textbox each time I check a value but I want to bring the textbox near each selected value that I retain in the label. So value1 textbox1, value2 textbox2....It's asp.net web application.
Why do you want to use the same label?
U could do:
foreach (ListItem item in this.CheckBoxList1.Items)
{
if (item.Selected)
{
Label label = new Label();
label.Text = item.Value;
TextBox txt1 = new TextBox();
form1.Controls.Add(label);
form1.Controls.Add(txt1);
}
}
you also might have to assign id to the textboxes if you are planning to retrieve user input from them.
But if you only have limited number of fields u could use static fields, and change its visibility as Tim stated
I don't really know what you plan to do but why dont you define a CSS class for both the label object and the textbox object using the same y- but alternate x-coordinates.

how to set the default value to the drop down list control?

I have a drop down list control on my web page. I have bind the datatable to the dropdownlist control as follows -
lstDepartment.DataTextField = "DepartmentName";
lstDepartment.DataValueField = "DepartmentID";
lstDepartment.DataSource = dtDept;
lstDepartment.DataBind();
in the page load event i want to set the default value to the drop down list control from my other table field.
how to do this?
After your DataBind():
lstDepartment.SelectedIndex = 0; //first item
or
lstDepartment.SelectedValue = "Yourvalue"
or
//add error checking, just an example, FindByValue may return null
lstDepartment.Items.FindByValue("Yourvalue").Selected = true;
or
//add error checking, just an example, FindByText may return null
lstDepartment.Items.FindByText("Yourvalue").Selected = true;
if you know the index of the item of default value,just
lstDepartment.SelectedIndex = 1;//the second item
or if you know the value you want to set, just
lstDepartment.SelectedValue = "the value you want to set";
Assuming that the DropDownList control in the other table also contains DepartmentName and DepartmentID:
lstDepartment.ClearSelection();
foreach (var item in lstDepartment.Items)
{
if (item.Value == otherDropDownList.SelectedValue)
{
item.Selected = true;
}
}
lstDepartment.DataTextField = "DepartmentName";
lstDepartment.DataValueField = "DepartmentID";
lstDepartment.DataSource = dtDept;
lstDepartment.DataBind();
'Set the initial value:
lstDepartment.SelectedValue = depID;
lstDepartment.Attributes.Remove("InitialValue");
lstDepartment.Attributes.Add("InitialValue", depID);
And in your cancel method:
lstDepartment.SelectedValue = lstDepartment.Attributes("InitialValue");
And in your update method:
lstDepartment.Attributes("InitialValue") = lstDepartment.SelectedValue;

Categories

Resources