Here is my code:
<form id="form1" runat="server">
<asp:GridView ID="gridv" AutoGenerateColumns="true" EnableViewState="true" runat="server" >
<Columns>
<asp:HyperLinkField runat="server" HeaderText="GetStudentInfo" SortExpression="GetStudentInfo" DataTextField="StudentName" NavigateUrl="StudentManagement2.aspx" />
</Columns>
</asp:GridView>
<asp:Button runat="server" Text="Click" OnClick="ClickPostback" />
</form>
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack) return;
gridv.DataSource = GetStudent();
gridv.DataBind();
}
When i click on the hyperlink on GridView - Postback is always false:
However when i click the Button Postback variable is True:
A post back occurs when a form gets submitted back to the server.
Hyperlinks are used for navigation, not form submission. So by default they redirect the user to a new page and do not post any information back to the server, which is why the post back is showing as false.
In your example the hyperlink has a NavigateUrl property which is where you are telling the application to "go to this page". It's not sending any information to the server for processing.
Buttons however where designed to post information back to the server, which is why it is showing true.
Related
I did a lot of searching and cannot figure this out.
I have a ModalPopupExtender pop-up that I want to display when the user clicks a link DoSomething. The pop-up has a dropdown control that I then want to populate on the fly when the user asks to open the dialogue. This needs to happen server side via the code behind. Currently I am trying to do it via an OnClick event on the link but as soon as the link is tied to the ModalPopupExtender the link OnClick code is not executed.
Code snippet:
<asp:LinkButton ID="lnkDoSomething" runat="server" onClick="lnkDoSomething_Click">Do Something</asp:LinkButton>
<asp:ModalPopupExtender ID="mpelnklnkDoSomething" runat="server" BackgroundCssClass="modalBackground"
DropShadow="true" PopupControlID="lnkDoSomething"
PopupDragHandleControlID="pnlDragHandlerForlnkDoSomething"
TargetControlID="lnklnkDoSomething"></asp:ModalPopupExtender>
The problem is as soon as I set the ModalPopupExtender to the link the OnClick code does not execute. This obviously is by design but it doesn't make sense to me (naive) as if the user clicks the link the OnClick code should be executed.
Any ideas why this is not supported and what the correct solution is?
Attach the ModalPopupExtender to a dummy button and show the modal on the LinkButton's OnClick even from the code-behind:
Markup:
<asp:LinkButton ID="lnkDoSomething" runat="server" onClick="lnkDoSomething_Click">Do Something</asp:LinkButton>
<asp:Button id="dummyButton" runat="server" style="display:none;" />
<asp:ModalPopupExtender ID="mpelnklnkDoSomething" runat="server"
BackgroundCssClass="modalBackground" DropShadow="true" PopupControlID="controlToPopUpId"
PopupDragHandleControlID="pnlDragHandlerForlnkDoSomething"
TargetControlID="dummyButton"></asp:ModalPopupExtender>
Code-behind:
protected void lnkDoSomething_Click(Object sender, EventArgs e)
{
//do work
mpelnklnkDoSomething.Show();
}
I am new at AJAX When i enter the web page it gives an error which is
``(c:\Users\Acer\Desktop\RSS_proje\RSS_proje\WebApplication2-5.2\ShareRss.aspx
var lblMsg = $get('<%=lblMessage.ClientID%>');){"Control 'ScriptManager1' of type 'ScriptManager' must be placed inside a form tag with runat=server."}
javascript side
HTML code[code:html]<asp:ScriptManager ID="ScriptManager1" runat="server">
function onCancel() {
var lblMsg = $get('<%=lblMessage.ClientID%>');
lblMsg.innerHTML = "You clicked the <b>Cancel</b> button of AJAX confirm.";
}
also, button side
<asp:Button ID="Button1" runat="server" Text="Share RSS" BackColor="#FF6600"
BorderColor="#FF9933" BorderStyle="Solid" Font-Bold="True" ForeColor="White"
Width="78px" onclick="Button1_Click" />
<ajaxToolkit:ConfirmButtonExtender ID="cbe" runat="server"
TargetControlID="Button1"
ConfirmText="Are you sure you want to share this?"
OnClientCancel="CancelClick" />
<asp:Label ID="lblMessage" runat="server"></asp:Label><br />
C# SIDE
protected void Button1_Click(object sender, EventArgs e)
{
lblMessage.Text = "You clicked the <b>OK</b> button of AJAX confirm";
}
If the ScriptManager tag is formatted exactly as you have it posted then make sure you're providing the appropriate closing tag for it. If you already are could you post a bit more of your markup so we can gat a better picture of how your Dom is structured
I'm trying to do a job filter for the list of jobs on our website. The filter for the job type is wrapped in an UpdatePanel and the button to apply the filters redirects back to the same page.
This is because I will be using the umbraco.library:RequestQueryString in the XSLT to populate the jobs list.
However, the querystring filter value doesn't seem to select the RadioButtonList. For example:
The page loads, but nothing happens because vt is null:
protected void Page_Load(object sender, EventArgs e)
{
string vt = Request.QueryString["vt"];
if (vt != null)
{
foreach (ListItem li in rblVacancyType.Items)
{
if (li.Value == vt)
{
li.Selected = true;
}
}
}
}
<asp:UpdatePanel ID="upSearchFilters" runat="server">
<ContentTemplate>
<p>
<asp:RadioButtonList ID="rblVacancyType" runat="server">
<asp:ListItem Text="All" Value="all"></asp:ListItem>
<asp:ListItem Text="Permanent" Value="permanent"></asp:ListItem>
<asp:ListItem Text="Temporary" Value="temporary"></asp:ListItem>
</asp:RadioButtonList>
</p>
</ContentTemplate>
</asp:UpdatePanel>
Here's the button:
<asp:ImageButton ID="ibFilters" ImageUrl="~/images/buttons/filter-button.png" OnClick="ibApplyFilters_Click" runat="server" />
Here's the procedure:
protected void ibApplyFilters_Click(object sender, EventArgs e)
{
Response.Redirect("/careers/join-us/?filters=true&vt=" + rblVacancyType.SelectedValue.ToString());
}
Yet when the page redirects the first time, nothing is selected, I click permanent, permanent gets selected. If I then select 'All' or 'Temporary' the selection doesn't change.
Any ideas?
Based on the behavior (it works the first time) I believe this describes what's happening:
MyPage.aspx (original load)
Page controls initialized to default
Page Load - No query string, no radio button selected
(user clicks button - causes postback)
MyPage.aspx (postback)
Page controls initialized to default
Radio Button Set from ViewState
Page Load - No query string, no radio button selected
ButtonClick - uses radio button setting, does response redirect
MyPage.aspx?VT=Permanent (load from redirect)
Page controls initialized to default
Page Load - Query string set, radio button selected
(user clicks button - causes postback)
MyPage.aspx?VT=Permanent (postback)
Page controls initialized to default
Radio Button Set from ViewState
Page Load - Query string set, radio button set to Permanent (Here is the problem)
ButtonClick - uses radio button setting, does response redirect
I believe a simple (if !IsPostback) will fix things
Sounds like postback values being re-applied. See this article on ASP.NET page lifecycle: http://msdn.microsoft.com/en-us/library/ms178472.aspx.
Values for controls are re-applied via postback after page_load.
Due to the strange nature of code + postbacks the logic in Mark's answer seems to be quite accurate, but the suggested fix did not work as I had tried that as a possible solution. The below is a modification, but as far as I could see it works. Give it a try, it might work out for you.
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="upSearchFilters" runat="server">
<ContentTemplate>
<p>
<asp:RadioButtonList ID="rblVacancyType" runat="server"
AutoPostBack="True">
<asp:ListItem Text="All" Value="all"></asp:ListItem>
<asp:ListItem Text="Permanent" Value="permanent"></asp:ListItem>
<asp:ListItem Text="Temporary" Value="temporary"></asp:ListItem>
</asp:RadioButtonList>
</p>
<p>
<asp:ImageButton ID="ibFilters" runat="server" CausesValidation="False"
Height="30px" OnClick="ibApplyFilters_Click" />
</p>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
Code Behind:
protected void Page_Load(object sender, EventArgs e)
{
ibFilters.PostBackUrl = "~/WebForm1.aspx?filters=true&vt=" + rblVacancyType.Text;
string vt = Request.QueryString["vt"];
}
Important:
The way this is set up, it will maintain your selection, update the filter parameters in the url on button push, and then assign vt the correct value to be used on filtering anything else on your page.
You must change out my "~/WebForm1.aspx?filters=true&vt=" for your url as well.
I need to display a control consistently across a set of pages. So I'm using a MasterPage to do that in ASP.NET/C#. However I also need to programmatically access this control, mostly provide options to view/hide depending on whether the controls checkbox is clicked.
Here is the Source for the MasterPage
<div id="verifyInitial" runat="server">
<asp:CheckBox ID="chkInitialVerify" runat="server"
oncheckedchanged="chkInitialVerify_CheckedChanged" />
I agree that my initial data is correct.
</div>
<div id="verifyContinuous" runat="server">
<asp:CheckBox ID="chkContinuousVerify" runat="server"
oncheckedchanged="chkContinuousVerify_CheckedChanged" />
I agree that my continuous data is correct
</div>
Now in the code behind I want to perform the following operations. Basically if a person clicks on the checkbox for the initial div box, then the initial box disappears and the continous box shows up. However it seems that the code behind for the MasterPage does not activate whenver I click on the checkbox. Is that just the way MasterPages are designed? I always thought you could do add some sort of control functionality beyond utilizing the Page Load on the Master Page.
protected void chkInitialVerify_CheckedChanged(object sender, EventArgs e)
{
verifyContinuous.Visible = true;
verifyInitial.Visible = false;
}
protected void chkContinuousVerify_CheckedChanged(object sender, EventArgs e)
{
verifyContinuous.Visible = false;
}
If you're expecting the two controls to trigger a change for that page immediately then you'll need to set the AutoPostBack property to true for both of them:
<div id="verifyInitial" runat="server">
<asp:CheckBox ID="chkInitialVerify" runat="server" oncheckedchanged="chkInitialVerify_CheckedChanged" AutoPostBack="true" />
I agree that my initial data is correct.
</div>
<div id="verifyContinuous" runat="server">
<asp:CheckBox ID="chkContinuousVerify" runat="server" oncheckedchanged="chkContinuousVerify_CheckedChanged" AutoPostBack="true" />
I agree that my continuous data is correct
</div>
Otherwise, you need an <asp:button /> or some other control on the page to trigger a postback and cause your event handlers to run. The button, or other control, can either be on your masterpage or on your content page, the choice is entirely yours.
In my web page i used a gridview. In this gridview it shows a group of users information.
I just added one button from smart tag menu. And my requirement is that when i am hitting the button corresponding to each users, it will redirect to another page and shows the corresponding user's information. What i do for getting this type of output?
U have to add the button and add an attribute CommandName:
<asp:Button ID="EditBtn" runat="server" CommandName="Edit" />
then in the event of itemcommand of the grid do the following
protected void GridView1_ItemCommand(object source, GridViewCommandEventArgs e)
{
if (e.CommandName == "Edit")
{
//Redirect to user page information
Response.Redirect(PageURL);
}
}
Ahmy's answer is the way to go if you want to use a button and have your page redirect to another page with the user's information. One thing that was left out however was that you can pass a command argument through the button (like the user's unique ID) which you could then put in the querystring of the page you are redirecting to, to identify which user it is. That would look like this:
<asp:TemplateField HeaderText="Edit User">
<ItemTemplate>
<asp:Button ID="EditBtn" Text="Edit User" CommandName="Edit"
CommandArgument='<%# Eval("UserID") %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
Then in the code behind
protected void GridView1_ItemCommand(object source, GridViewCommandEventArgs e)
{
if (e.CommandName == "Edit")
{
//Redirect to user page information
Response.Redirect("UserProfilePage.aspx?userID=" + e.CommandArgument);
}
}
Another alternative to using a button, which I think is the best option is to use a HyperLinkField. When using a button, the page will have to post back to the server, then send a redirect to the user's browser. With a hyperlink, the user goes straight to the correct page. It saves a step and doesn't rely on javascript.
<asp:HyperLinkField DataTextField="UserName" DataNavigateUrlFields="UserID"
DataNavigateUrlFormatString="UserProfilePage.aspx?userID={0}"
HeaderText="Edit User" />
Instead of button, make one of the column hyperlinked. On clicking the item, redirect to your new page (using Javascript). By this you can avoid an additional column for the button and a postback.
You must use DataTextFormatString for this.
Sample
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="no" HeaderText="SNo" />
<asp:BoundField DataField="file" DataFormatString="<a href=javascript:ShowAssetDetail('{0}');>{0}</a>"
HeaderText="Asset Desc" HtmlEncodeFormatString="False" />
</Columns>
</asp:GridView>
In above sample the JS function ShowAssetDetail() must take a value to pass to the redirecting page. Needless to say, the JS function must be written in addition.