want asp dropdownlist selection to make panel visible - asp.net

I have an asp dropdownlist that I would like to make a panel visible if the selection contains a certain word- how would this be possible?

lets say you have dictionary like this
List<string> words = new List<string>();
words.Add("foo");
then onchange event of drop down list
string selectedText = ddlPanel.SelectedText;
foreach(var w in words)
{
if ( w.Contains(selectedText)
{
pnl.Visible = true;
}
}

<select onchange="if (this.options[this.selectedIndex].value.indexOf('foo') != -1) document.getElementById('panel').style.display = 'block'">

Place the panel you're looking to hide within an update panel, and use the dropdownlist change as a trigger to the updatepanel. On the update, check the dropdownlist's value and set the visibility.
Either that, or if you know the ID of the panel you can manually use javascript and bind the change event to a function that checks the values and shows/hides the panel accordingly.

Create dropDownlist with two items, "visible" and "not visible" or whatever suits you and make sure to set the autopostback property to true.
Then in vb write the following on page load:
If ddlMydropdown.Text = "visible" then
panelId.Visible = true
else
panelId.Visible = false
End If
If you code in c#, you can convert this vb code to c# over at developerfusion

Related

Asp Checkboxlist Loses Values After PostBack

I have a checkboxlist control that I need to check the values of when a Submit button is clicked by the user onscreen(C#).
This checkboxlist is part of a user control that I am referencing within my page markup.
However, when I check the values of the checkboxlist within the code of the submit button, all of the values are gone (i.e it says there are no items at all in the checkboxlist control).
Anyone know why this would be happening? I am doing the exact same thing in code in another place with another checkboxlist user control and it works perfectly.
I don't have my exact code to hand but below is a simplified version of what I am doing.
Basically I am binding data to the Checkboxlist only when it is NOT a postback on the usedr control.
USER CONTROL WHICH CONTAINS ONLY THE CHECKBOXLIST CONTROL Page_Load()
If(!IsPostBack)
{
foreach(var item in myVals)
{
ListItem i = new ListItem();
i.Text = item.Text;
i.Value = item.Value;
i.Selected = false;
myCheckBoxListControl.Add(i);
}
}
Now I have a submit button function which checks the values in the checkboxlist...
SubmitButton_Click()
{
foreach(ListItem item in myCheckBoxListControl.Items)
{
// process each one here. The code never gets in here as there are never any items in the checkboxlist
}
}
Anyone know why the CheckboxList has lost all of its items by the time the submit button function gets executed? The checkboxlist has EnableViewState set to true.

Dynamically adding Textbox in asp.net using C#

I have written a code in which a textbox is dynamically added to a gridview cell. There is some default texts in the textbox. I want that when users will click on the textbox the default text will disappear and the user can then write anything on it only in number, i.e. the user will not be able to use letters or special characters.
Kindly let me know how to achieve this.
Code example
Gridview gv=new Gridview();
gv.DataSource=dt;
gv.DataBind();
Textbox t1 = new Textbox();
t1.Text="Outages if any(in mins)";
gv.Rows[0].Cells[0].Controls.Add(t1);
Need help after this, something like when user puts his cursor in the textbox , the default text will disappear , and if the user removes the cursor without writing anything , the default text will reappear. Also the default text should be a bit blurred
Thanks.
Try something like this
Textbox t1 = new Textbox();
t1.Attributes.Add("onclick", "if(this.value == 'default text') this.value = '';"
t1.Attributes.Add("onblur", "if(this.value == '') this.value = 'default text';" />
You could also use onfocus in case users use tab key
With this, using this.value, approach you don't need to know the client ID of the control.
Here is a post describing exactly what you are looking to do:
HowTo: including default text in a Textbox while enforcing server-side validation
The pertinent points are:
Adding javascript attribute to onfocus & and onblur:
txtName.Attributes.Add("onfocus","clearText()");
txtName.Attributes.Add("onblur","resetText()");
Adding the javascript to clear and repopulate the textbox:
function clearText() {
document.form1.txtName.value = ""
}
function resetText() {
if(document.form1.txtName.value == "")
document.form1.txtName.value = "(enter something here)"
You can do this. The easiest way is to use the ajaxControlToolkit. You can create controls dynamically. For example:
Dim mt As new TextBox
Dim newTest As New AjaxControlToolkit.TextBoxWatermarkExtender
With mt
.ID = "textBox1"
.TextMode = TextBoxMode.SingleLine
End With
With newTest
.ID = "TextBoxWatermarkExtender1"
.TargetControlID = mt.ClientID
.WatermarkText = "test"
End With
Then just add both controls to the gridview as you are doing with the textbox. If you are not using Ajax, you can add javascript to the control through codebehind but this is more difficult. Let me know if that is what you want to do and Ill add some code to show you how.
This should create a textbox control with an associated ajax TextBoxWaterMarkExtender.

How to specify ControlID attribute of Ajax Update Panel for dynamically created controls

If a control such as checkboxlist is created dynamically.
like this
CheckBoxList CbxList = new CheckBoxList();
CbxList.ID = "Cbx";
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
CbxList.Items.Add(new ListItem(ds.Tables[0].Rows[i]["Name"]
.ToString(), ds.Tables[0].Rows[i]["ID"].ToString()));
}
ph.Controls.Add(CbxList);
If on the selectedIndexChange event of the created checkboxlist(cbx) if i am updating an ajax update panel how should i specify the ControlID Attribute. I did try Cbx in my case but it says no controls and off course that control is not created yet. So how to handle this issue
The only thing that I can think of needing a control ID is the triggers collection; if that is the case, why not put the checkboxlist within the updatepanel, and use an updatemode="always" on the panel itself?
If I understand your question, you want each checkbox to have an ID?
In that case, don't use a CheckBoxList, but use place holder instead, and add CheckBox controls to it.
However, I think the way you do it, they should get IDs such as 'Cbx_0','Cbx_1',...

Why is my RadioButtonList selectedValue empty?

I have a RadioButtonList:
<asp:RadioButtonList ID="rblMedicationTime" runat="server" onselectedindexchanged="rblMedicationTime_SelectedIndexChanged" DataSourceID="dtsMedicationTime" DataTextField="LookupItem" DataValueField="Id" AutoPostBack="true"></asp:RadioButtonList>
On page load, I want to select a radio button from the list and set its value, for which I have written this line of code:
rblMedicationTime.SelectedValue = clientMedicationSchedule.glTypeId.ToString();
The RadioButtonList is populating successfully, but the value is unable to be selected.
rblMedicationTime.SelectedValue is always "" when I debug the code.
You just need to use
string myValue = myRadioButtonList.SelectedItem.Value
The property object myRadioButtonList.SelectedItem contains all values from the selected item of a Radio Button list or a DropDown list
to set the value programmatically all you have to do is:
myRadioButtonList.SelectedIndex = 0;
You can see that you have several ways to Get but only one to Set:
myRadioButtonList.SelectedIndex --> Gets or Sets
myRadioButtonList.SelectedValue --> Gets
myRadioButtonList.SelectedItem --> Gets
You can't set the selected radiobutton with .SelectedValue, only with .SelectedIndex.
Check MSDN (at SelectedValue it says "Gets the value", at SelectedIndex is says "Gets or sets")
I think problem in !IsPostBack.
if (!IsPostBack)
{
string str = rblMedicationTime.SelectedValue;
}
First you should check !IspostBack
hello friend there is something another problem in your code because in my side this is running fine.
rblMedicationTime.SelectedValue = clientMedicationSchedule.glTypeId.ToString();
can u cehck that clientMedicationSchedule.glTypeId.ToString() this contain value or not.
and on page load u put selection code on if (!IsPostBack){} block.

How to put values in textbox from listbox in jQuery MVC ASP.NET

I have one Listbox named "List1" and one button says "Append".
I have on textbox named "TextDescription".
I want to put the select values from the listbox to textbox on click of append button.
So can anyone tell me how to do this?
You could use the .val() function. So assuming you have a select with id="myselect" and a text input with id="mytext" you could do this:
var values = $('#myselect').val();
if (values != null) {
// concatenate the selected values with , so that we can add them to the textbox
$('#mytext').val(values.join(','));
}
And here's a live demo illustrating it in action.

Resources