I have a UserControl that contains a CheckBox and a TextBox:
<asp:CheckBox runat="server" ID="chk1" />
<asp:TextBox runat="server" ID="tb1" />
On Page_Load I'm adding several of them dynamically them to a Panel on the page:
//loop through the results from DB
foreach (Thing t in Things)
{
//get the user control
MyUserControl c1 = (MyUserControl )Page.LoadControl("~/UserControls/MyUserControl.ascx");
//set IDs using public properties
c1.ID = "uc" + t.ID;
c1.CheckBoxID = "chk" + t.ID;
cl.TextBoxID = "tb" + t.ID;
//add it to the panel
myPanel.Controls.Add(c1);
//add the event handler to the checkbox
((CheckBox)myPanel.FindControl(c1.ID).FindControl(c1.CheckBoxID)).CheckedChanged += new EventHandler(CheckBox_CheckedChanged);
}
I then created the method for the event handler in the same page:
protected void CheckBox_CheckedChanged(object sender, EventArgs e)
{
string test = "breakpoint here";
}
When I put a breakpoint inside CheckBox_CheckedChanged it's never hit when the my checkbox gets clicked.
When I look at the view source, this is the code that gets generated:
<input id="ctl00_body_uc1_chk1" type="checkbox" name="ctl00$body$uc1$chk1" checked="checked" />
So, it doesn't seem to be picking up when I add the event handler. It's weird, cause it picks up everything else though.
Am I missing something?
Add the CheckBox.AutoPostBack property and set it to "true".
CheckBox cb = ((CheckBox)myPanel.FindControl(c1.ID).FindControl(c1.CheckBoxID));
if(cb != null)
{
cb.AutoPostBack = true;
}
"When I put a breakpoint inside CheckBox_CheckedChanged it's never hit when the my checkbox gets clicked."
If you want the event to fire when the check box gets clicked, you also need to set AutoPostBack = true on the check box. If you put the cursor in the text box and press return (causing a post back), does the event fire?
Related
I want to make RadioButtons work in a ListView. I added a RadioButton control in the ListView and wrapped it with a LinkButton control. I used the LinkButton in order to use the ItemCommand property of the the ListView to change the state of the RadioButton from code Behind.
<td>
<asp:LinkButton ID="LinkButton6" runat="server" CommandName="Driver">
<asp:RadioButton ID="SelectedDriver" runat="server" AutoPostBack="true" OnCheckedChanged="SelectedDriver_CheckedChanged" />
</asp:LinkButton></td>
To be able to make all radioButton mutually exclusive, I reset all radioButtons in the ListView to false then I set the RadioButton that call the event to true
Here is the method from code behind:
protected void Drivers_ItemCommand(object sender, ListViewCommandEventArgs e)
{
if (e.CommandName.ToString() == "Driver")
{
foreach (ListViewItem listItem in this.Drivers.Items)
{
(listItem.FindControl("SelectedDriver") as RadioButton).Checked = false;
}
(e.Item.FindControl("SelectedDriver") as RadioButton).Checked = true;
}
}
However it doesn't work as I wanted. when I click on the radioButtons, they keep getting selected none of them get reset. For it to work, I have to add some text beside the radioButton control and I have to click on the text not on the RadioButton for it to work. As you can see the text part is pretty annoying.
Can anyone help me to know what i can do for it to work as intended? Or Does anyone knows a solution that can work.
Thanks
For the RadioButtons to work as expected, I set the AutoPostBack = "true" the property OnCheckedChanged = "SelectedDriver_CheckedChanged"
from code behind I did the following and everything works perfectly
protected void SelectedDriver_CheckedChanged(object sender, EventArgs e)
{
RadioButton selectedButton = new RadioButton();
selectedButton = (RadioButton)sender;
foreach (ListViewItem listItem in this.Drivers.Items)
{
(listItem.FindControl("SelectedDriver") as RadioButton).Checked = false;
}
selectedButton.Checked = true;
}
I have a number of radio buttons created dynamically inside a loop and added to a div inside update panel. On each postback request triggered by checked change event, I recreate the radio buttons in my page_init method. My problem is the radio button I selected is not checked and the checked changed event is not firing on first click. But on subsequent clicks, it works normally and the checked changed event is fired. Only the first click is not firing. What could be the issue?
Simple dynamic radio button.
RadioButton btn2 = new RadioButton();
btn2.Text = "TEST";
btn2.CheckedChanged += Btn2_CheckedChanged; ;
btn2.AutoPostBack = true;
pricetbldiv.Controls.Add(btn2);
private void Btn2_CheckedChanged(object sender, EventArgs e)
{
RadioButton btn = (RadioButton)sender;
string text = btn.Text;
}
Try to assign group and ID
btn2.ID = "Text";
btn2.Text = "Text";
btn2.GroupName = "RB";
btn2.CheckedChanged += new EventHandler(Btn2_CheckedChanged);
I want to create a dynamic button when click a pop up appear and user key in then submit.
First of all I have dynamic button created depends on the table row
TableCell tc;
for (int i = 1; i < Approval_TBL.Rows.Count; i++)
{
TableRow tr = Approval_TBL.Rows[i];
tr.Cells.Add(tc = new TableCell());
Button tb = new Button();
tb.ID = Convert.ToString(i);
tb.Text = "Reject";
tb.CssClass = "btn btn-default";
tb.OnClientClick = "javascript:$find('popup1').show();return false;__doPostBack('btnReject_click','')";
tb.Click += new EventHandler(btnReject_click);
tc.Controls.Add(tb);
}
then I have the popup created using ajax:
<ajaxToolkit:ModalPopupExtender ID="ModalPopupExtender1" runat="server"
TargetControlID="1"
BehaviorID="popup1"
PopupControlID="Panel1"
DropShadow="true"
CancelControlID="Button3"
OnOkScript="OkButtonClick"
BackgroundCssClass="BackgroundStyle" />
then I have created click event
protected void btnReject_click(object sender, EventArgs e)
{
//code
}
Currently looks like it only run Onclientclick event where the popup appear but it wont go in to btnReject_click. but if i remove the onclientclick, it will be able to go in the btnReject_click.
My way of doing is weird because i dont know how to use asp.net and most of my code i put it in server side (c#).
if you using jQuery then
$("selector").on('click',function(){
// your code here
});
Within your OnClientClick you return false;. Anything after that is not executed so the postback never happens. Remove the return and the postback should fire.
tb.OnClientClick = $find('popup1').show();__doPostBack('btnReject_click','');";
I am creating a custom view in a SharePoint visual Web Part using ASP.NET (Visual C#) and have a CheckBoxList, and a button.
MarkUp for the List & Button:
<td>
<asp:checkboxlist ID="cblYearLst" runat="server" EnableViewState="true" />
</td>
<td>
<asp:Button ID="btnRefineSearch" Text="Refine Search" runat="server" />
</td>
I add items to the CheckBoxList on PreRender:
if (!IsPostBack)
{
if (LstYears != null)
{
for (int i = 0; i < LstYears.Count(); i++)
{
cblYearLst.Items.Add(new ListItem(LstYears[i], LstYears[i]));
}
}
}
And I call the event Handler for the button on Page_Load:
btnRefineSearch.Click += new EventHandler(this.btnRefineSearch_Click);
All of the CheckBox list-items do not stay selected after the button is clicked. I can retrieve the selected values, but they won't display as selected. When I add the Click event handler for the button in the pre-render event, the data is displayed appropriately but the selected values can no longer be retrieved by my Click event.
Any ideas on what might be causing this behaviour??
Did you try moving the binding of the checkboxlist into the page_load instead of pre_render? Just an idea because it seems like the page is losing selections on postback and you are regenerating the options each time.
UPDATE: I created a quick page and this works correctly. Do you have your viewstate turned off for the entire page in your page directive, or possibly in the web.config? I see you have it enabled on the checkboxlist but maybe there is a global setting throwing you off.
protected void Page_Load(object sender, EventArgs e)
{
btnRefineSearch.Click += new EventHandler(this.btnRefineSearch_Click);
List<string> LstYears = new List<string>();
LstYears.Add("one");
LstYears.Add("two");
LstYears.Add("three");
LstYears.Add("four");
if (!IsPostBack)
{
if (LstYears != null)
{
for (int i = 0; i < LstYears.Count; i++)
{
cblYearLst.Items.Add(new ListItem(LstYears[i], LstYears[i]));
}
}
}
}
private void btnRefineSearch_Click(object sender, EventArgs args)
{
Response.Write(cblYearLst.SelectedValue);
}
I figured out the issue, since I have AutoEventWireUp set to true as Keenan has suggested it should all work if I do it in the page_load.
The problem was that Page_Load was being called twice, and I discovered that this was because I was redirecting the user to the same URL with QueryString parameters. After I made the much needed changes, my code works very well.
+1 Keenan for your help and thank you (#jfmags) for tipping me off by telling me you think it is something else.
:D
i am creating a tab container at runtime and make 1 of the column member as the tab header
once user click on the tab i want to fire the event ActiveTabChanged.. if autopostback=true the entire tab container will gone but it got commend inside the event. if autopostback=false it can't go in the event and nothing happen at the layout.. hence i change the concept of my code..what i want is when user click on tab.. event are fire > everything remain same > next asp.net function will be call from the event. below are my coding
Remark- TabC is the tab container
<script type="text/javascript">
function ActiveTabChanged(sender, e) {
var Current_Tab = $get('<%#TabC.ClientID%>');
Current_Tab.innerHTML = sender.get_activeTab().get_headerText();
__doPostBack('TabC', sender.get_activeTab().get_headerText());
//Highlight(TabC);
}
</script>
in the body
<asp:TabContainer ID="TabC"
runat="server"
OnClientActiveTabChanged="ActiveTabChanged"
OnActiveTabChanged="ActiveTabChangedServer"
ActiveTabIndex="0"
/>
at Page Load
UpdatePanel oUpdatePanel = new UpdatePanel();
AsyncPostBackTrigger trigger = new AsyncPostBackTrigger();
protected void Page_Load(object sender, EventArgs e)
{
trigger.ControlID = "TabC";
trigger.EventName = "ActiveTabChanged";
oUpdatePanel.Triggers.Add(trigger);
Page.Header.DataBind();
ScriptManager.RegisterAsyncPostBackControl(TabC);
if (!Page.IsPostBack)
{
//runtime generate tab and gridview
Bind_Category_with_Tab();
}
}
error i get
output
the main problem is.. when i fire any event of the component in the page every things will disappear.. if the event can be trigger (AutoPostBack="true")
what mistake i have been done here? please to give a help here.. thanks advance