I have a Radiobuttonlist. In items i was adding Specify your own value with One textbox. I f i clicked on that text-box radio-button should select b y default.
<td style="text-align:left" class="contract_value_bg" width="50%">
<asp:RadioButtonList ID="rblDocumentstType" runat="server" AutoPostBack="true" OnSelectedIndexChanged="rblDocumentstType_SelectedIndexChanged" RepeatColumns="1">
</asp:RadioButtonList>
<asp:TextBox ID="txtRFP" runat="server" AutoPostBack="true" OnTextChanged="txtRFP_TextChanged" MaxLength="120" />
</td>
enter image description here
You want to set focus on the textbox if "specify your own value" was clicked in the RadioButtonList? Or you want to select that item in the RadioButtonList if the user clicked into the TextBox?
This is for the former case:
protected void rblDocumentstType_SelectedIndexChanged(Object sender, EventArgs e)
{
RadioButtonList rblDocumentstType = (RadioButtonList) sender;
if(rblDocumentstType.SelectedIndex == 1)
{
txtRFP.Focus();
}
}
If you want to select the second RadioButtonList item if the TextBox got focus you should do that at client-side by handling the onfocus event with javascript (or jQuery):
<asp:TextBox ID="txtRFP" runat="server" onfocus="selectSpecifyYourOwn()" AutoPostBack="true" OnTextChanged="txtRFP_TextChanged" MaxLength="120" />
function selectSpecifyYourOwn() {
var rbID = '<%=rblDocumentstType.ClientID %>';
var rb = document.getElementById(rbID);
var items = rb.getElementsByTagName("input");
items[1].checked = true;
}
Related
My code:
<asp:DataList ID="datalist" runat="server" >
<ItemTemplate>
<asp:Textbox ID="Values" runat="server" type="text" />
</ItemTemplate>
</asp:DataList>
<asp:Button ID="Button1" runat="server" Text="SEND" OnClick="send" />
How could I get each Values ID elements of the DataList from code behind in C# ?
You loop all the items in the DataList and use FindControl to locate the TextBox.
protected void send(object sender, EventArgs e)
{
//loop all the items in the datalist
foreach (DataListItem item in datalist.Items)
{
//find the textbox with findcontrol
TextBox tb = item.FindControl("Values") as TextBox;
//do something with the textbox content
string value = tb.Text;
}
}
I want to show multiple text boxes depending on the number a user selects from a dropdown box. Example below:
So whatever number is selected in the drop down the page refreshes and displays that number of text boxes. I need to go up to 20 fields. Is there a way to do this in C#, or maybe with the Ajax Control Toolkit?
ASPX
<asp:Label ID="NumAccounts" runat="server" Text="# of Accounts"></asp:Label> <asp:DropDownList
ID="EmpNameList" runat="server" onselectedindexchanged="NumAccountsList_SelectedIndexChanged"
AutoPostBack="True">
</asp:DropDownList>
sure there is.
use:
int ctrlCount=Convert.ToInt32(DropDownList1.SelectedItem.Value);
int ctrlTopPos=30;
lbl_name.Text="Name:";
for(int i=0;i<ctrlCount;i++)
{
Label lbl_name=new Label();
TextBox txt_cur=new TextBox();
txt_cur.Top=ctrlTopPos+(i*30);
lbl_name.top=ctrlTopPos+(i*30);
txt_cur.left=lbl_name.Width+30;
Panel1.Controls.Add(lbl_name);
Panel1.Controls.Add(txt_cur);
}
Create an asp:panel and name it Panel1.
Put the given code inside the SelectedIndexChanged event of your dropdownlist.
set the autopostback property of your dropdownlist to true.
it will work.
hope that helps.
try this
<asp:Label ID="NumAccounts" runat="server" Text="# of Accounts"></asp:Label> <asp:DropDownList
ID="EmpNameList" runat="server" onselectedindexchanged="NumAccountsList_SelectedIndexChanged"
AutoPostBack="True">
</asp:DropDownList>
<div>
<asp:PlaceHolder id="ContentPlaceHolder1" runat="server" />
</div>
protected void NumAccountsList_SelectedIndexChanged(object sender, EventArgs e)
{
ContentPlaceHolder1.Controls.Clear();
for(i=0; i<Convert.ToInt32(EmpNameList.SelectedItem.Value); i++)
{
TextBox tx= new TextBox();
tx.ID="tx"+i;
ContentPlaceHolder1.Controls.Add(tx);
ContentPlaceHolder1.Controls.Add(new LiteralControl("<br />"));
}
}
I'm trying to setup a usercontrol.
I've setup a usercontrol with a table incl table header. For each row a second usercontrol is issued displaying 1 row of the table. The line-usercontrol is embedded in a listview.
One cell of the listview contains a textbox, another cell contains a Button. The buttons for each row are using the same buttonclick event using a commandargument for finding the pressed button.
Is there somekind of way to get the value entered in the textbox of that specific line?
code header control:
<asp:Panel runat="server" ID="pnlUcHeader">
<table>
<thead>
<tr>
<th>...</th>
...
<th>Textbox Column</th>
<th>Button Column</th>
</tr>
</thead>
<asp:ListView runat="server" ID="lvUcItemsItem" OnItemDataBound="lvUcItems_ItemDataBound">
<ItemTemplate>
<uc1:Items runat="server" ID="ucItems" CssClass="normal" />
</ItemTemplate>
</asp:ListView>
</table>
code Item User control
<tr runat="server" id="serverRow">
<td>
<asp:Literal runat="server" ID="..."></asp:Literal></td>
<td>
<asp:TextBox runat="server" ID="tbItem"></asp:TextBox>
</td>
<td>
<asp:Button runat="server" ID="btnItem" Text="..." OnCommand="btnItem_Click"/>
</td>
code behind Item User control
protected void btnItem_Click(object sender, CommandEventArgs e)
{
var lineID = e.CommandArgument;
//TextBox tb = FindControl("tbAfhaalDatum)
???????
}
Is this possible in code behind or is there an option to use Javascript/Jquery to postback the value?
Kind regards
You have to access your control's NamingContainer to search for your text box:
protected void btnItem_Click(object sender, CommandEventArgs e)
{
var control = (Control)sender;
var container = control.NamingContainer;
var textBox = container.FindControl("tbItem") as TextBox;
if (textBox != null)
{
var lineID = e.CommandArgument;
var text = textBox.Text;
}
}
Following is the test scenario for my code.
1) Once the user selects one of the radio buttons on Webpage.aspx, a modal popup extender shows up.
2) A user control (SSL_Ticket.ascx) is defined inside the modal popup window.
3) A RequiredFieldValidator is defined for a drop down list contained inside the user control.
4) If the user selects the "0" value from drop down list, no validation error message is displayed.
Code
Webpage.aspx
<asp:RadioButtonList ID="RadioButtonListForTicket" runat="server" AutoPostBack="true"
OnSelectedIndexChanged="radioButtonListForTicket_OnSelectedIndexChanged">
<asp:ListItem Selected="True">No</asp:ListItem>
<asp:ListItem>Yes</asp:ListItem>
</asp:RadioButtonList>
<ajaxToolkit:ModalPopupExtender ID="ModalPopupExtenderForTicket" runat="server" BackgroundCssClass="popUpStyle"
DropShadow="true" PopupControlID="divTicketPreview" PopupDragHandleControlID="panelDragHandle"
TargetControlID="btnForPopupAppear" CancelControlID="btnForPopupDisappear"/>
....
...
Webpage.aspx.cs
protected void radioButtonListForTicket_OnSelectedIndexChanged(object sender, System.EventArgs e)
{
if (RadioButtonListForTicket.SelectedItem.Text.ToString().Equals("Yes"))
{
// Check if the sites are selected
updateSelectionCount();
updateListOfSites();
if (selectionCount == 0)
{
lblSSLTicketSelection.Text = "Please select a site.";
RadioButtonListForTicket.SelectedValue = "No";
return;
}
else
{
lblSSLTicketSelection.Text = "";
}
....
ModalPopupExtenderForTicket.Show();
}
}
...
SSL_Ticket.ascx
<asp:DropDownList ID="cmbRootCause" runat="server" Width="255px" OnSelectedIndexChanged="cmbRootCause_SelectedIndexChanged" AutoPostBack="true"
CausesValidation="true">
<asp:ListItem Value="0">Select</asp:ListItem>
<asp:ListItem Value="1">Item1</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="reqdFieldForRootCause" runat="server" ControlToValidate="cmbRootCause" InitialValue="Select"
ErrorMessage="Please select root cause" ValidationGroup="validateRootCause" Visible="false" Display="Dynamic" EnableClientScript="true">
</asp:RequiredFieldValidator>
...
SSL_Ticket.ascx.cs
protected void cmbRootCause_SelectedIndexChanged(object sender, EventArgs e)
{
if (cmbRootCause.SelectedItem.ToString().Equals("Other"))
{
lblcmbRootCause.Text = "";
lblcmbRootCause.Visible = false;
txtRootCauseOther.Visible = true;
}
else if (cmbRootCause.SelectedItem.ToString().Equals("Select"))
{
lblcmbRootCause.Visible = true;
lblcmbRootCause.Text = "Please select root cause";
}
else
{
lblcmbRootCause.Text = "";
lblcmbRootCause.Visible = false;
txtRootCauseOther.Visible = false;
}
}
I did browse through couple of solutions (ValidateProperty, Client-side validation, RangeValidation, etc), but it did not fire validation text.
This did not help - Handling RequiredFieldValidator inside of a User Control
I'd appreciate your help very much.
Thanks!!!
Remove visible = false attribute from required field validator, by default they won't show up in the beginning.
<asp:RequiredFieldValidator ID="reqdFieldForRootCause" runat="server" ControlToValidate="cmbRootCause" InitialValue="Select"
ErrorMessage="Please select root cause" ValidationGroup="validateRootCause" **Visible="false"** Display="Dynamic" EnableClientScript="true">
</asp:RequiredFieldValidator>
Well in your 'RequiredFieldValidator' for your DropDownList you need to remove this:
InitialValue="Select"
I have created a DataList in asp.net -
<asp:DataList runat="server" ID="pTextBox">
<ItemTemplate>
<asp:CheckBox ID="CheckBoxPN" runat="server" Checked='false' />
<asp:TextBox ID="profileTextBox" runat="server" Text='<%# Container.DataItem.ToString() %>'></asp:TextBox>
</ItemTemplate>
</asp:DataList>
This creates checkBoxes and textBoxes based on the string values passed through from a webService.
How can I get the profileTextBox Text string value when a user clicks CheckBoxPN and populate another textBox outwith the DataList on the page with the string value??
You can use the CheckedChanged event of the CheckBox and cast it's NamingContainer to DataListItem, the you just have to use FindControl to find a different server control:
protected void CheckBoxPN_CheckedChanged(Object sender, EventArgs e)
{
CheckBox chk = (CheckBox) sender;
DataListItem item = (DataListItem) chk.NamingContainer;
TextBox txt = (TextBox) item.FindControl("profileTextBox");
this.OtherTextBoxOnPage.Text = txt.Text; // here we are
}
By the way, this approach works with any web-databound control(Repeater, GridView, etc.)