DefaultButton in MultiView.ActiveViewChanged Event - c#

I am using a multiview control in asp.net, and on ActiveViewChanged event I want set default button programmatically as of the selected view so am using code:
if(myMultiview.GetActiveview() == myView)
this.Page.Form.DefaultButton = btnDefault.UniqueID;
here btnDefault is exist in myView, even though it throws exception 'System.NullReferenceException'
please suggest me the solutions

I had similar type of problem, And i try this,
<asp:Panel id="myPanel" runat="server" DefaultButton="ButtonID">
<asp:MultiView ID="MultiView1" runat="server" ActiveViewIndex="0">
<asp:View ID="View1" runat="server" >
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
</asp:View>
<asp:View ID="View2" runat="server">
<asp:Button ID="Button2" runat="server" Text="Button" onclick="Button2_Click" />
</asp:View>
</asp:MultiView>
</asp:Panel>
</div>
</form>
protected void Page_Load(object sender, EventArgs e)
{
myPanel.DefaultButton = "Button1";
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write("View1 Button is clicked");
MultiView1.ActiveViewIndex = 1;
myPanel.DefaultButton = "Button2";
}
protected void Button2_Click(object sender, EventArgs e)
{
Response.Write("View2 Button is clicked");
MultiView1.ActiveViewIndex = 0;
}
may be help you.

Related

Asp.net define enter button action

I have 2 buttons in my webpage.When i press "Enter",it auto perform "close_topic",but i want my enter button to perform "button_click".
<asp:Button ID="btnLogout" Text="Close Topic" OnClick="close_topic" runat="server" />
<asp:Button ID="ButtonTT" Text="Click" runat="server" OnClick="button_click" />
protected void button_Click(object sender, EventArgs e)
{
}
protected void close_topic(object sender, EventArgs e)
{
}
tried to use <asp:Panel ID="Panel1" runat="server" DefaultButton="ButtonTT">,but not working.
Did you wrap your panel around the 2 buttons? As follows:
<asp:Panel ID="Panel1" runat="server" DefaultButton="ButtonTT">
<asp:Button ID="btnLogout" Text="Close Topic" OnClick="close_topic" runat="server" />
<asp:Button ID="ButtonTT" Text="Click" runat="server" OnClick="button_click" />
</asp:Panel>
Edit
Based on how to set a default 'enter' on a certain button, you can do it using code also:
Me.Form.DefaultButton = Me.btn.UniqueID;
or
Me.Page.Form.DefaultButton = = Me.btn.UniqueID;
Replacing the Me.Page with whatever your page name is.

.NET Validators doesn't work

I have simple page Page.aspx like this:
<asp:Content ID="Content2" runat="server">
<asp:PlaceHolder ID="phNewAddress" runat="server" />
</asp:Content>
In code behind is:
protected override void Page_Load(object sender, EventArgs e)
{
AddressDetail xControl = (AddressDetail)Page.LoadControl("AddressDetail.ascx");
phNewAddress.Controls.Add(xControl);
}
AddressDetail.ascx is:
<asp:TextBox ID="address_name" runat="server" />
<asp:RequiredFieldValidator ID="rfv" runat="server"
ControlToValidate="address_name" ErorMessage="Required" ValidationGroup="save">*
</asp:RequiredFieldValidator>
<asp:DropDownList ID="address_state" runat="server" AutoPostBack="true" />
<asp:Button ID="btnSave" runat="server" Text="Save" ValidationGroup="save" OnClick="btnSave_Click" />
DropDownList address_state is binding from datasource.
Now. After page is loaded and I press Save button, validator doesn't work.
But if I change DropDownList, so fire postback, and then press Save button, validator works fine.
Can anyone help me?
I am guessing validation controls work with viewstate, Move the below code from Page_load event to Page_Init event and see if it works.
protected override void Page_Init(object sender, EventArgs e)
{
AddressDetail xControl = (AddressDetail)Page.LoadControl("AddressDetail.ascx");
phNewAddress.Controls.Add(xControl);
}

Change image linkbutton on click

Having this piece of code:
<asp:LinkButton runat="server" OnClick="ChangeImage_Click" >
<img src="imagethatshouldbechanged" />
</asp:LinkButton>
And this:
protected void ChangeImage_Click(object sender, EventArgs e)
{
//Code here
}
How do I change the image when I click the linkbutton.
You need to make img runat="server" and assign id to it to access in ChangeImage_Click or use asp:Image instead of img
HTML
<asp:LinkButton runat="server" OnClick="ChangeImage_Click" >
<img id="img1" runat="server" src="imagethatshouldbechanged" />
</asp:LinkButton>
Code bahind
protected void ChangeImage_Click(object sender, EventArgs e)
{
img1.Src = "url";
}
Try this way
<asp:LinkButton runat="server" OnClick="ChangeImage_Click" >
<img runat="server" id="myImage" src="imagethatshouldbechanged" />
</asp:LinkButton>
protected void ChangeImage_Click(object sender, EventArgs e)
{
myImage.Src="YourImagePath";//Like App/app1/image1.jpeg
}

response.redirect not working within update panel using timer tick

I need to redirect page using a button within a update panel. but when I try this It didn't work for me. can anyone help me on this??/. Please Find My Code Below.
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<div>
<asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Label ID="Label2" runat="server" Text="Label" Width="345px"></asp:Label><br />
<br />
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Button2" EventName="Click" />
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
</Triggers>
</asp:UpdatePanel>
<asp:Timer ID="Timer1" runat="server" ontick="Timer1_Tick" Interval="500">
</asp:Timer>
<br />
<asp:Button ID="Button1" runat="server" Text="Button" Width="344px" OnClick="Button1_Click" /><br />
<asp:Button ID="Button2" runat="server" Text="Button" />
<br />
<br />
</div>
</form>
</body>
My Code Behind Code
public partial class test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
}
public void imgBtn_Click(object sender, EventArgs e)
{
Response.Redirect("~/HotelResult.aspx");
}
protected void Timer1_Tick(object sender, EventArgs e)
{
Timer1.Enabled = false;
Label1.Text = DateTime.Now.ToString();
Label2.Text = DateTime.Now.ToString();
Label3.Text = DateTime.Now.ToString();
ImageButton testbtn = new ImageButton();
testbtn.ID = "testbtnid";
testbtn.Click += new System.Web.UI.ImageClickEventHandler(this.imgBtn_Click);
testbtn.ImageUrl = "images/book-btn.png";
PlaceHolder1.Controls.Add(testbtn);
}
}
The problem here is that ImageButton is added to the page only after timer tick. If you want server-side handlers to be invoked for such dynamically added controls, you have to added the to the page on every postback. Here is how it can be done using key view state:
protected void Page_Load(object sender, EventArgs ea)
{
bool? addHotelResultButton = ViewState["AddHotelResultButton"] as bool?;
if (addHotelResultButton.HasValue && addHotelResultButton.Value)
{
AddHotelResultButton();
}
}
protected void Timer1_Tick(object sender, EventArgs e)
{
ViewState["AddHotelResultButton"] = true;
AddHotelResultButton();
}
protected void AddHotelResultButton()
{
Timer1.Enabled = false;
Label1.Text = DateTime.Now.ToString();
Label2.Text = DateTime.Now.ToString();
Label3.Text = DateTime.Now.ToString();
ImageButton testbtn = new ImageButton();
testbtn.ID = "testbtnid";
testbtn.Click += new System.Web.UI.ImageClickEventHandler(this.imgBtn_Click);
testbtn.ImageUrl = "images/heroAccent.png";
PlaceHolder1.Controls.Add(testbtn);
}
I would do it like this :
Put your button inside your updatePanel and disable it
Put it as a trigger inside the <triggers> tag
When the tick event come, enable your button

Modal popup not generating postback to page

<ajaxToolkit:ModalPopupExtender runat="server"
id="ModalPopupExtender1"
cancelcontrolid="btnCancel" okcontrolid="btnOkay"
targetcontrolid="Button1" popupcontrolid="Panel1"
drag="true"
backgroundcssclass="ModalPopupBG"
/>
<asp:Button ID="Button1" runat="server" Text="Test Modal Popup"
onclick="Button1_Click" />
<br />
<asp:UpdatePanel ID="up" runat="server">
<ContentTemplate>
<asp:Button ID="Button2" runat="server" Text="Post Back"
onclick="Button2_Click" />
<asp:Label ID="Label1" runat="server" AutoPostBack="true" Text="Nothing has happened yet..."></asp:Label>
<asp:Panel ID="Panel1" runat="server" AutoPostBack="true">
<div class="HellowWorldPopup">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="btnCancel" runat="server" Text="Canel"
onclick="btnCancel_Click" />
<asp:Button ID="btnOkay" runat="server" Text="Okay" onclick="btnOkay_Click" />
</div>
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
So I am trying to get the Label to have the contents of what the user typed in the textbox inside the modal popup. Right now the btnOkay is not causing this to work.
.cs file:
protected void btnCancel_Click(object sender, EventArgs e)
{
TextBox1.Text = "";
}
protected void btnOkay_Click(object sender, EventArgs e)
{
Label1.Text = TextBox1.Text;
TextBox1.AutoPostBack = true;
}
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = "";
TextBox1.Text = "";
}
protected void Button2_Click(object sender, EventArgs e)
{
Label1.Text = "You clicked the button";
}
I do not want the page to post back at all, but just to update the hidden labels on the page once information is entered. How do I do this?
Edit:
Alright, this should do the trick I think.
<ajax:ModalPopupExtender runat="server" id="ModalPopupExtender1" targetcontrolid="Button1"
popupcontrolid="Panel1" drag="true" backgroundcssclass="ModalPopupBG" />
<asp:Button ID="Button1" runat="server" Text="Test Modal Popup" /><br />
<asp:Panel ID="Panel1" runat="server">
<div class="HellowWorldPopup">
<asp:TextBox ID="TextBox1" runat="server" />
<asp:Button ID="btnCancel" runat="server" Text="Canel" onclick="btnCancel_Click" />
<asp:Button ID="btnOkay" runat="server" Text="Okay" onclick="btnOkay_Click" />
</div>
</asp:Panel>
<asp:UpdatePanel ID="up" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Label ID="Label1" runat="server" Text="Nothing has happened yet..." />
</ContentTemplate>
</asp:UpdatePanel>
.
protected void btnCancel_Click(object sender, EventArgs e)
{
TextBox1.Text = "";
}
protected void btnOkay_Click(object sender, EventArgs e)
{
Label1.Text = TextBox1.Text;
up.Update();
}

Categories

Resources