I'm using ASP.NET and I'm dynamically filling up my DropDownList.
Here is my code:
DataTable dtList = new DataTable();
dtList.Columns.Add("Name");
dtList.Columns.Add("Type");
foreach (DataDefinitionResponse dr in _dr)
{
if (dr.Type == "Dropdown")
{
string[] strSplit = dr.ListValue.Split('|');
List<string> lst = new List<string>();
foreach (string word in strSplit)
{
DataRow row = dtList.NewRow();
row["Name"] = word;
row["Type"] = dr.Name;
dtList.Rows.Add(row);
}
}
}
ddlFieldList.DataSource = dtList;
ddlFieldList.DataTextField = "Name";
ddlFieldList.DataValueField = "Type";
ddlFieldList.DataBind();
Now I just want to hide a specific item using Javascript when another DropDownList is selected.
I'm not using SelectedIndexChanged here. I must use Javascript.
Please someone help me with this.
Thx
I don't think you will be able to manipulate the DropDownList with JavaScript and "get away with it" because when the page is subsequently posted back to the server ASP .NET will detect that the DropDownList has been "tampered" with and will throw an exception.
There are flags you can set that stop the error but it is unlikely that you will then be able to use the DropDownList in your code-behind.
You would normally achieve what you are trying to do with SelectedIndexChanged (I know you said you didn't want to) and put the control in an UpdatePanel or similar to avoid a full page post-back / refresh.
function Remove()
{
var DropDownList=document.getElementById('<%=DropDownList1.ClientID%>');
alert(DropDownList.value);
for(i=DropDownList.length-1; i>=0; i--)
{
if(DropDownList.options[i].selected)
{
DropDownList.remove(i);
}
}
}
Related
I've dynamically bonded select list and when I hit save button then I'm getting value 0 not the selected one.
I'm Using HtmlSelect not Asp:Dropdownlist.
Can anybody help me.?
thanks in advance !!
If you are using html select.,
you use javascript function for change ddl and assign changed ddl value at one hidden field. send that hidden field value to server. Check below code.
function onchangeddl(e) {
var ddl = document.getElementById('ddlid')
for (var i = 0; i < ddl.options.length; i++) {
if (ddl.options[i].text == e.target.options[e.target.options.selectedIndex].text) {
ddl.selectedIndex = i;
ddl.options[i].selectedValue = e.target.options[e.target.options.selectedIndex].value;
ddl.options[i].selected = true;
document.getElementById('<%=hdnfld.ClientID%>').value = e.target.options[e.target.options.selectedIndex].text;
break;
}
}
}
Codebehind.aspx page you have to assign hdnfld value.
I hope its helpful to you.
I am using Watin to test this page but I am unable to get to the subpage of each row in the table.
http://www.domea.dk/sog-bolig/Ledige-boliger/Sider/default.aspx
Any idea how to loop through each row and click the link to the subpage.
I am able to get a hold of the table that holds each row using:
Table table_name = browser.Table(Find.ById("listTable"));
And then loop through each row using:
foreach (TableRow currRow in table_name.TableRows)
{
var s = currRow.TableCell(Find.ByIndex(0));
}
But I don't know how to get the "onclick" event inside of the cell to be "clicked" using watin.
If you want to click on each row link this is the code you need to use:
using (var browser = new IE("http://www.domea.dk/sog-bolig/Ledige-boliger/Sider/default.aspx"))
{
Table table_name = browser.Table(Find.ById("listTable"));
foreach (TableRow currRow in table_name.TableRows)
{
foreach (var c in currRow.TableCells)
{
if (c.Links.Count > 0)
{
c.Links[0].Click();
}
}
}
}
You were going good but needed to get to the link on each row before you can click on it. In sample code I'm using the first link found on the cell c.Links[0].Click(); if this is going to be used as a production code you need to check that the first link is the one you need, for example, checking that the URL of the link is going to xxxx or it contains a specific web page (.Contains) etc.
I have a TreeView that is displayed inside a panel. The data in the TreeView is based on data returned from the database. The first time, the data is correct. The second time, the TreeView is not refreshed, and the previous data is still showing in the tree. I checked the list that contain the data. The list returned the correct data. I've Google the issue, and could not resolved it with some of the answers that were posted. Here is a sample code of how the TreeView is being created and added to the Panel.
ReportGroups gr = new ReportGroups();
var Name = gr.GetReportName(groupID);
TreeView tr = new TreeView();
tr.BeginUpdate();
tr.Size = new Size(570, 600);
tr.Name = "Home";
tr.Nodes.Add("Reports Name");
tr.CheckBoxes = true;
if (Name.Count() > 0)
{
foreach (var item in Name)
{
if (item != null)
{
tr.Nodes[0].Nodes.Add(item.reportName);
}
}
}
tr.Nodes[0].ExpandAll();
tr.EndUpdate();
this.pDisplayReportName.Width = tr.Width * 2;
this.pDisplayReportName.Height = 300;
this.pDisplayReportName.Controls.Add(tr);
this.pDisplayReportName.Refresh();
What am I doing wrong?
try to add this.pDisplayReportName.Clear(); so data will not double up. :)
The easy option would be to use this.pDisplayReportName.Controls.Clear(); just after tr.EndUpdate();. But, this would cause an issue if you have other controls within the same Panel.
The best option would be to use this.pDisplayReportName.Controls.RemoveByKey("MyTree"); instead of this.pDisplayReportName.Controls.Clear();
And, another option would be to add a TreeView in design time (with name tr) rather than dynamically to the panel. Then, use tr.Nodes.Clear(); before tr.BeginUpdate(); and remove following two lines from your code.
TreeView tr = new TreeView();
.
.
.
this.pDisplayReportName.Controls.Add(tr);
Cheers
I'm binding to a dropdown. It works on the initial load. On subsequent loads (postbacks) it doesn't refresh the items in the dropdown.
using (DataView dv = dtProductGroup.DefaultView)
{
dv.ApplyDefaultSort = false;
dv.Sort = "KVIGroupName ASC";
ddlGroup.ClearSelection();
ddlGroup.Items.Clear();
string strAll = Localization.GetResourceValue("_strddlStatusLBAll");
ddlGroup.DataValueField = "KVIGroupId";
ddlGroup.DataTextField = "KVIGroupName";
ddlGroup.DataSource = dv;
ddlGroup.DataBind();
ListItem item = new ListItem(strAll, "0");
ddlGroup.Items.Insert(0, item);
}
I've confirmed that on the postbacks the data is being bound to the dropdown and items are successfully added. But when the page renders the dropdown doesn't have any of the new values.
I see two possibilities: The control isn't rendering the new values or the values are being cleared. I'm at a loss of where to look for possible problems.
Edit
I discovered the problem. The dropdownlist was embedded in an Conditional UpdatePanel. Simply calling "UpdatePanel.Update();" solved the problem.
Upon Postback the viewstate is being reapplied + you said you're trying to load values again. I'd suggest letting viewstate carry all the weight on postback. Only load the values when the page is first hit by adding if (! IsPostBack) like so
using (DataView dv = dtProductGroup.DefaultView)
{
if (! IsPostBack) {
dv.ApplyDefaultSort = false;
dv.Sort = "KVIGroupName ASC";
ddlGroup.ClearSelection();
ddlGroup.Items.Clear();
string strAll = Localization.GetResourceValue("_strddlStatusLBAll");
ddlGroup.DataValueField = "KVIGroupId";
ddlGroup.DataTextField = "KVIGroupName";
ddlGroup.DataSource = dv;
ddlGroup.DataBind();
ListItem item = new ListItem(strAll, "0");
ddlGroup.Items.Insert(0, item);
}
}
Edit:
Also, your syntax ensures the DataView object referenced by dv is Disposed of when the code block exits. My second guess is this causes a side-effect that causes the problem.
using (DataView dv = dtProductGroup.DefaultView)
{
Instead leave out the using and write a regular declaratoin like the following (The DataView is going to be Disposed along with everything else when the page is done rendering so there's not really any need to do it yourself).
DataView dv = dtProductGroup.DefaultView;
See the MSDN documentation about 'using' and IDisposable for detailed info.
I've already got my custom header drawing in my GridView using SetRenderMethodDelegate on the header row within the OnRowCreated method. I'm having problems trying to add LinkButtons to the new header row though.
This is what the RenderMethod looks like:
private void RenderSelectionMode(HtmlTextWriter output, Control container)
{
TableHeaderCell cell = new TableHeaderCell();
cell.Attributes["colspan"] = container.Controls.Count.ToString();
AddSelectionModeContents(cell);
cell.RenderControl(output);
output.WriteEndTag("tr");
HeaderStyle.AddAttributesToRender(output);
output.WriteBeginTag("tr");
for(int i = 0; i < container.Controls.Count; i++)
{
DataControlFieldHeaderCell cell = (DataControlFieldHeaderCell)container.Controls[i];
cell.RenderControl(output);
}
}
private void AddSelectionModeContents(Control parent)
{
// TODO: should add css classes
HtmlGenericControl label = new HtmlGenericControl("label");
label.InnerText = "Select:";
selectNoneLK = new LinkButton();
selectNoneLK.ID = "SelectNoneLK";
selectNoneLK.Text = "None";
//selectNoneLK.Attributes["href"] = Page.ClientScript.GetPostBackClientHyperlink(selectNoneLK, "");
//selectNoneLK.Click += SelectNoneLK_Click;
selectNoneLK.Command += SelectNoneLK_Click;
selectAllLK = new LinkButton();
selectAllLK.ID = "SelectAllLK";
selectAllLK.Text = "All";
//selectAllLK.Attributes["href"] = Page.ClientScript.GetPostBackClientHyperlink(selectAllLK, "");
//selectAllLK.Click += SelectAllLK_Click;
selectAllLK.Command += SelectAllLK_Click;
parent.Controls.Add(label);
parent.Controls.Add(selectNoneLK);
parent.Controls.Add(selectAllLK);
}
As you can see, I have tried different ways to get my LinkButtons working (none have worked though). The LinkButtons are rendered as plain anchor tags, like this: <a id="SelectNoneLK">None</a>
I know there is something wrong with the fact that the ID looks like that, since I am using a Master page for this and the ID should be something much longer.
Any help would be appreciated!
Nick
I'd guess that since cell is not part of the control hierarchy (you never add it to the table), the LinkButton's never find an IContainer parent to rewrite their ID's.
I tend to solve these types of issues using the excellent RenderPipe control that allows me to declare my controls in one place, but render them somewhere else.