In my project i have kept source info in dropdownlist. Like source of info: in dropdownlist three items website, Newspaper and others. If user select Others item, then only other text box should be visible otherwise should be invisible. For that i have set in page load event
lblother.visible=false;
txtother.visible=false;
And in Btnsubmit event i have written the condition like.
if(dropdownlistinfo.selectedindex==2)
{
lblother.visible=true;
txtother.visible=true;
}
But in my case i m not getting my desire output. Its always invisible when i am selecting Others item from drowdownlist also. Pls somebody help me where is my mistake?
Thanks,
Sumit
I think the problem is here.
if (!IsPostBack)
{
lblother.visible = false;
txtother.visible = false;
}
This will work if you set Selected property of the default list item.
<asp:DropDownList ID="DropDownList" runat="server">
<asp:ListItem Text="Website" Selected="True"></asp:ListItem>
<asp:ListItem Text="Newspaper"></asp:ListItem>
<asp:ListItem Text="Other"></asp:ListItem>
</asp:DropDownList>
<asp:Label ID="lblOther" runat="server" Text="Other"></asp:Label>
<asp:TextBox ID="txtOther" runat="server"></asp:TextBox>
Hide the controls in the Page Load event.
protected void Page_Load(object sender, EventArgs e)
{
this.txtOther.Visible = false;
this.lblOther.Visible = false;
}
Then show the controls in the button click event.
protected void Button1_Click(object sender, EventArgs e)
{
if (DropDownList1.SelectedIndex == 2)
{
this.txtOther.Visible = true;
this.lblOther.Visible = true;
}
}
Related
I have following code in my application I have a gridview control insde my grid view I have radio button defined as templetefield.
<asp:View ID="View3" runat="server">
<script type = "text/javascript">
function RadioCheck(rb) {
var gv = document.getElementById("<%=grdAllPartsRequestList.ClientID%>");
var rbs = gv.getElementsByTagName("input");
var row = rb.parentNode.parentNode;
for (var i = 0; i < rbs.length; i++) {
if (rbs[i].type == "radio") {
if (rbs[i].checked && rbs[i] != rb) {
rbs[i].checked = false;
break;
}
}
}
}
</script>
<asp:TemplateField>
<ItemTemplate>
<asp:RadioButton ID="rbSelected" runat="server" AutoPostBack="True"
oncheckedchanged="rbSelected_CheckedChanged" GroupName="RequestSelection" onclick="RadioCheck(this);" />
<asp:HiddenField ID="HiddenField1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</<asp:View>
Code Behind .CS
protected void rbSelected_CheckedChanged(object sender, EventArgs e)
{
}
Now my problem is upon running my page when I click the radio button the Event doesn't fire. oncheckedchanged is not firing.
Surprisingly The same functionality works fine in another page with in the grid view.
I have searched the web for this answers. But so far nothing worked out. I have Update Panel in my Master Page.. I have tried to play with UpdateMode="Always" and ChildrenAsTriggers="true" but so far nothing worked out please help!
Try this,
void GridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
Radiobutton rb = e.Row.FindControl("rbSelected") as Radiobutton;
rb.oncheckedchanged += this.rbSelected_CheckedChanged;
}
}
The event will be added to the radiobutton on rowdatabound event of GridView...
Hope this helps you...
I think this is a "doh" moment caused by me not having dome WebForms dev for a few years..
I have a repeater which which contains a bunch of checkboxes:
<asp:Repeater EnableViewState="true" ID="IDTypesRepeater" runat="server" OnItemDataBound="IdTypesRepeaterItemDataBound">
<HeaderTemplate/>
<ItemTemplate>
<asp:CheckBox EnableViewState="true" ID="chkIdType" Text="<%# ((KeyValuePair<string,int>)Container.DataItem).Key %>" runat="server" />
<asp:HiddenField ID="idType" Value="<%# ((KeyValuePair<string,int>)Container.DataItem).Value %>" runat="server"/>
<br />
</ItemTemplate>
</asp:Repeater>
I need to get the checkboxes that are selected in the code behind:
foreach (RepeaterItem repeaterItem in IDTypesRepeater.Items)
{
if ( ((CheckBox)repeaterItem.FindControl("chkIdType")).Checked )
{
// Do something
}
}
But on postback, this code isn't working! I know about always databinding a repeater, so I've done this:
protected void Page_Load(object sender, EventArgs e)
{
IDTypesRepeater.DataSource = DocTemplateHelper.GetApplicableIDTypes().Where(type => type.Value != 0);
IDTypesRepeater.DataBind();
}
So this repopulates the repeater, but the Update code never finds any checked checkboxes.. Any ideas?
Bind in the Page_Init event
protected void Page_Init(object sender, EventArgs e)
{
IDTypesRepeater.DataSource = DocTemplateHelper.GetApplicableIDTypes().Where(type => type.Value != 0);
IDTypesRepeater.DataBind();
}
Be sure to use the !Page.IsPostBack method in your pageload.
Otherwise, the Repeater will keep getting reset, and all your checkboxes
will be in there default value (unchecked)
This should fix it. You are binding the control on postback hence losing the values. You can bind it after handling any event to show the updated record.
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
IDTypesRepeater.DataSource = DocTemplateHelper.GetApplicableIDTypes().Where(type => type.Value != 0);
IDTypesRepeater.DataBind();
}
}
I have nested repeaters on my aspx page.In the outer repeater I am displaying a list of products and in the inner repeater I am displaying a list of additional options associated with each product.The inner repeater contains a checkbox,textbox,label and other stuff.I would like to find the controls inside the outer repeater when a user selects a checkbox in the inner repeater.In order to handle this I am using the following code.
<asp:Repeater ID="OuterRepeater" runat="server"
onitemdatabound="OuterRepeater_ItemDataBound" >
<ItemTemplate>
<asp:Label ID="CodeLabel" runat="server" Text='<%# Eval("Code") %>'></asp:Label>
<asp:Repeater ID="InnerRepeater" runat="server" OnItemCreated="InnerRepeater_ItemCreated">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="true"/>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
......
.......
</ItemTemplate>
</asp:Repeater>
......
......
</ItemTemplate>
</asp:Repeater>
protected void InnerRepeater_ItemCreated(object sender, RepeaterItemEventArgs e)
{
RepeaterItem ri = (RepeaterItem)e.Item;
if (ri.ItemType == ListItemType.Item || ri.ItemType == ListItemType.AlternatingItem
)
{
CheckBox cb = ri.FindControl("CheckBox1") as CheckBox;
cb.CheckedChanged += new EventHandler(CheckBox1_CheckedChanged);
}
}
private void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
CheckBox cb = (CheckBox)sender;
if (cb.Checked)
{
//do something
}
else
{
//do something
}
}
But the checkedChanged event of the checkbox is not firing for some reason.Also I am not sure how to access the textbox of the outer repeater in the checked changed event of the innter repeater checkbox control.
Could someone please help me with this?
Thanks
It does not fire the CheckedChanged event, since you have declared the event handler as private, You have to make it Protected or Public
Protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
You can access the Textbox control like..
private void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
CheckBox checkBox = (CheckBox)sender;
Textbox textbox1 = (TextBox)checkBox.Parent.FindControl("TextBox1");
String textboxText = textbox1.Text;
}
It doesn't look like you defined an event handler in your markup.
<asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="true" OnCheckedChanged="CheckBox1_CheckedChanged" />
Muhammad Akhtar's answer helped me a lot today!
I just needed to set a specific ID to my dynamic generated checkboxes inside my reapeater to recover the origin of the event, and do the rest of the processing, and it worked perfectly.
chkAtivo.ID = DataBinder.Eval(e.Item.DataItem, "id").ToString();
Reovered just as the sample.
Cant vote up yet, but thank you.
I have a datapager with a pagertemplate. In the template I have a "Show All" button, which sets the PageSize of the datapager to show all records. This works fine but I want to be able to hide the button when it's clicked. It's in an UpdatePanel so I don't know if that makes a difference?
<asp:DataPager ID="Pager" runat="server" PagedControlID="rangeList" PageSize="15" EnableViewState="false">
<Fields>
<asp:TemplatePagerField>
<PagerTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CommandArgument="<%# Container.TotalRowCount.ToString() %>"
oncommand="LinkButton1_Command" >Show All Ranges</asp:LinkButton>
</PagerTemplate>
</asp:TemplatePagerField>
<asp:numericpagerfield ButtonCount="10" NextPageText="..." PreviousPageText="..." CurrentPageLabelCssClass="pageOn" />
</Fields>
</asp:DataPager>
And the codebehind:
protected void LinkButton1_Command(object sender, CommandEventArgs e)
{
this.Pager.PageSize = int.Parse(e.CommandArgument.ToString());
LinkButton lb = (LinkButton)sender;
if (lb != null)
{
lb.Visible = false;
}
rangeList.DataBind();
}
The first click works fine, and refreshes the ListView which in turn adjusts the pager to show one page with all the results on it, but the button doesn't disappear as I want it to.
Any ideas?
If there's nothing to display within the pager, why not hide the Pager control itself:
protected void LinkButton1_Command(object sender, CommandEventArgs e)
{
this.Pager.PageSize = int.Parse(e.CommandArgument.ToString());
this.Pager.Visible = false;
lnkShowPages.Visible = true; // EDIT only
rangeList.DataBind();
}
EDIT:
You could have a second "Show Pages" LinkButton that's initially not visible and becomes visible when the Show All LinkButton is clicked (above). When this new LinkButton is clicked, it could then enable paging by setting the Pager's PageSize and visibility and hiding itself:
protected void lnkShowPages_Command(object sender, CommandEventArgs e)
{
this.Pager.PageSize = int.Parse(e.CommandArgument.ToString());
this.Pager.Visible = true;
lnkShowPages.Visible = false;
rangeList.DataBind();
}
in the modal pop up i am using detailsview control by default all the data would
be shown in label.
there will be an edit button down once the user clicks edit button all the lablel would be gone and text box and dropdown control should be present so that user can change the values and again update into database
looking forward for a solution. i dnt want to use sqlDatasource. i wanted it to do in .cs
thank you
here is how to:
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" />
</EditItemTemplate>
protected void DetailsView1_DataBound(object sender, EventArgs e)
{
if (DetailsView1.CurrentMode == DetailsViewMode.Edit)
{
DropDownList ddl = DetailsView1.FindControl("DropDownList1") as DropDownList;
if (ddl != null)
{
ddl.DataSource = dataSource;
ddl.DataBind();
}
}
}