In my application i have gridview with edit button. By clicking edit button in gridview the listitem of DropDownList gets replaced by the text that is in gridview these are my dropdown list values
--select--
Roller
Heater
Aspx Code:
<asp:DropDownList ID="ddlvalue" runat="server" Width="175px" AppendDataBoundItems="true">
<asp:ListItem>Select</asp:ListItem>
<asp:ListItem>Roller</asp:ListItem>
<asp:ListItem>Heater</asp:ListItem>
</asp:DropDownList>
CS Code:
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
ddlValue.SelectedItem.Text = (GridView1.SelectedRow.FindControl("lblValue") as Label).Text;
}
here is what you should do. you care currently changing the text of selected item instead of finding the correct item and selecting it. here is how you should do it
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
string value=(GridView1.SelectedRow.FindControl("lblValue") as Label).Text;
try
{
foreach(ListItem li in ddlValue.Items)
{
if(li.Text==value)
{
li.Selected=true;
}
else
{
li.Selected=false;
}
}
}
catch
{
}
}
string value = (GridView1.SelectedRow.FindControl("lblRole") as Label).Text;
try
{
ddlvalue.ClearSelection();
ddlvalue.Items.FindByText(value).Selected = true;
}
catch
{
}
Related
I have 2 list boxes.
<asp:ListBox ID="ListBox_Region" runat="server"
DataTextField="arregion" DataValueField="arregion" AutoPostBack="True"
Height="96px"
Width="147px" DataSourceid="sqldatasource1"></asp:ListBox>
<asp:ListBox ID="ListBox_Area" runat="server"
DataTextField="ardescript" DataValueField="ardescript"
AutoPostBack="True"
OnSelectedIndexChanged="ListBox_Area_SelectedIndexChanged"
Height="96px"
Width="147px" >
So, when I select a value from ListBox_Region , the corresponding values get updated in ListBox_Area in this way:
protected void ListBox_Region_SelectedIndexChanged(object sender, EventArgs e)
{
this.ListBox_Area.Items.Clear();
string selectedRegion = ListBox_Region.SelectedValue;
var query = (from s in DBContext.areas
where s.arregion == selectedRegion
select s);
ListBox_Area.DataSource = query;
ListBox_Area.DataBind();
}
The event for ListBoxRegion_SelectedIndexChaged is written in page Load.
However, the problem is on initial page load, where the first value of ListBox_Region should be Selected by default. The second listbox should be updated to corresponding values but this should happen before selected index changed gets fired. So, can u please let me know how to do this?
Move the logic on ListBox_Region_SelectedIndexChanged to a separated method and call it from page_load when postback is false.
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
// Bind ListBox_Region and set the first value as selected
...
//
BindAreaList();
}
}
protected void ListBox_Region_SelectedIndexChanged(object sender, EventArgs e)
{
BindAreaList();
}
protected void BindAreaList()
{
this.ListBox_Area.Items.Clear();
string selectedRegion = ListBox_Region.SelectedValue;
var query = (from s in DBContext.areas
where s.arregion == selectedRegion
select s);
ListBox_Area.DataSource = query;
ListBox_Area.DataBind();
}
I have a problem regarding TReeview and combo box.
Problem:
I have a Treeview with Parent and Child Nodes.
I have a drop box or a combo box. Whenever I select a value from the combobox it should automatically select the same node in the treeview list.
Please give me suggestions on how to perform this.
This is what I tried so far:
protected void nav_dd_parent_SelectedIndexChanged(object sender, EventArgs e)
{
nav_treeview.selectedvalue = nav_dd_parent.selectedvalue.tostring();
}
But it says that nav_treeview is read only and it can not be assigned any values.
I just tried the following to give you an example:
HTML:
<asp:TreeView ID="TreeView1" runat="server">
</asp:TreeView>
<br />
<br />
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
Code Behind:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Dictionary<string, Int32> myList = new Dictionary<string, Int32>();
myList.Add("Text1", 1);
myList.Add("Text2", 2);
myList.Add("Text3", 3);
myList.Add("Text4", 4);
myList.Add("Text5", 5);
foreach (KeyValuePair<string, Int32> s in myList)
{
this.TreeView1.Nodes.Add(new TreeNode(s.Key, s.Value.ToString()));
this.DropDownList1.Items.Add(new ListItem(s.Key, s.Value.ToString()));
}
foreach (TreeNode tn in this.TreeView1.Nodes)
{
tn.ChildNodes.Add(new TreeNode("Hello World"));
tn.Collapse();
}
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.DropDownList1.SelectedItem != null)
{
foreach (TreeNode tn in this.TreeView1.Nodes)
{
if (tn.Value == this.DropDownList1.SelectedItem.Value)
{
tn.Selected = true;
if (tn.ChildNodes.Count > 0)
{
tn.Expand();
}
}
else {
tn.Collapse();
}
}
}
}
Hopefully this is what you needed. Good luck!
You will want to set AutoPostBack on the DropDownList and add an OnSelectedIndexChanged event handler. Or, you could try and write it all in javascript to avoid the post back.
I have the following code:
<asp:BulletedList ID="filingList" runat="server" DisplayMode="LinkButton"
onclick="filingList_Click">
</asp:BulletedList>
<asp:Literal ID="filingLiteral" runat="server"></asp:Literal>
and in the backend I fill the bulleted list with ListItems (where AlternateFileUrl is a url string that points to text formatted in html):
foreach (ShortFiling file in filingArray)
{
filingList.Items.Add(new ListItem(file.Type.ToString() + " "
+ file.Date.ToString(), file.AlternateHtmlFileUrl));
}
How do I access the text of the value of the item that's clicked on in the filingList then set it to the asp:Literal control? Here is the empty event handler that I defined and assume that I need to put the code in to set the asp:literal to the value of the specified ListItem.
protected void filingList_Click(object sender, BulletedListEventArgs e)
{
//put code here to set the asp:Literal text to
//the value of the item that is clicked on
}
protected void filingList_Click(object sender, BulletedListEventArgs e)
{
var value = filingList.Items[e.Index].Value;
filingLiteral.Text = value;
}
UPDATE 2
Ok you want the text from that URL, keep your markup as it was and change the code behind to this:
protected void filingList_Click(object sender, BulletedListEventArgs e)
{
var value = filingList.Items[e.Index].Value;
using(var client = new WebClient())
{
string downloadString = client.DownloadString(value);
filingLiteral.Text = downloadString;
}
}
You will need to add the System.Net namespace.
If I have understood your question correctly, you would just handle the click event of the BulletedList control, like:
protected void filingList_Click(object sender, BulletedListEventArgs e)
{
BulletedList bull = (BulletedList)sender;
ListItem li = bull.Items(e.Index);
filingLiteral.Text = li.Value;
}
I have a Listview control "lstStudents" and i have added checkboxes inside the List viewControl.I need to add a Select All check box which results in checking all the checkboxes inside the ListView i use the following code but it doesn't work.
private void chkAll_CheckedChanged(object sender, EventArgs e)
{
foreach (Control cont in lstStudents.Controls)
{
if (cont.GetType() == typeof(CheckBox))
{
(cont as CheckBox).Checked = true;
}
}
}
I'm using c# windows Forms......
You are talking to the dataitem instead of the control itself
private void chkAll_CheckedChanged(object sender, EventArgs e)
{
foreach (ListViewItem item in lstStudents.Items)
{
item.Checked = chkAll.Checked;
}
}
the Checked property is always accessible on a ListViewItem, visible or not.
lstStudents.Items returns only ListViewItem
so there is no need for an extra reference validation on these items
Try this:
private void chkAll_CheckedChanged(object sender, EventArgs e)
{
foreach (ListViewDataItem item in lstStudents.Items)
{
CheckBox cbSelect = item.FindControl("cbSelect") as CheckBox;
if (cbSelect != null)
{
cbSelect.Checked = true;
}
}
}
Assuming your listview definition goes something like this:
<asp:listview runat="server">
<itemtemplate>
<asp:checkbox id="cbSelect" runat="server" />
</itemtemplate>
</asp:listview>
I have to set a LinkButton's OnClientClick attribute but I don't know what this value is until the LinkButton is bound to. I'm trying to set the value when the repeater binds, but I can't workout how to get the 'boundItem/dataContext' value...
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<asp:LinkButton Text="HelloWorld" ID="Hyper1" runat="server" OnDataBinding="Repeater1_DataBinding" >
</asp:LinkButton>
</ItemTemplate>
</asp:Repeater>
protected void Page_Load(object sender, EventArgs e)
{
var list = new List<TestObject>();
list.Add(new TestObject() {TestValue = "testing1"});
list.Add(new TestObject() { TestValue = "testing2" });
list.Add(new TestObject() { TestValue = "testing3" });
this.Repeater1.DataSource = list;
this.Repeater1.DataBind();
}
public void Repeater1_DataBinding(object sender, EventArgs e)
{
var link = sender as HyperLink;
//link.DataItem ???
}
Is there anyway to find out what the current rows bound item is?
Maybe you need to use ItemDataBound event. It provides RepeaterItemEventArgs argument which has DataItem available
this.Repeater1.ItemDataBound += Repeater1_ItemDataBound;
void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
var dataItem = e.Item.DataItem;
}
I assume you are trying to get the value for the row that is currently being databound?
You can change your function to:
public void Repeater1_DataBinding(object sender, EventArgs e)
{
var link = sender as HyperLink;
string valueYouWant = Eval("TestValue").ToString();
// You could then assign the HyperLink control to whatever you need
link.Target = string.Format("yourpage.aspx?id={0}", valueYouWant);
}
valueYouWant now has the value of the field TestValue for the current row that is being databound. Using the DataBinding event is the best way to do this compared to the ItemDataBound because you don't have to search for a control and localize the code specifically to a control instead of a whole template.
The MSDN library had this as a sample event handler:
public void BindData(object sender, EventArgs e)
{
Literal l = (Literal) sender;
DataGridItem container = (DataGridItem) l.NamingContainer;
l.Text = ((DataRowView) container.DataItem)[column].ToString();
}
(see http://msdn.microsoft.com/en-us/library/system.web.ui.control.databinding.aspx)
As you can see it is a simple demonstration of how to access the data item and get data from it. Adapting this to your scenario is an exercise left to the reader. :)