I have a master page and have included a search text box.
User is on default.aspx
user enters a search value in the search text box, which is part of master page.
Via javascript the form, in MP, is submitted to my search functionality page, search.aspx and the code behind search.aspx.cs obtains the form post and gets the data.
Search.aspx has a bare bone table to display the results.
I have a breakpoint in search.aspx.cs and am able to see the raw results returned from DB. However after all databinding has occured, the user is not directed from default.aspx to search.aspx
Don't use any javascript to submit the form, just set the PostBackUrl property of your search button to Search.aspx:
<asp:TextBox ID="TxtSearch" runat="server" />
<asp:LinkButton
ID="BtnSearch"
runat="server"
PostBackUrl="Search.aspx"
Text="Search" />
And in the Page_Load of Search.aspx read the posted value:
protected void Page_Load(object sender, EventArgs e)
{
string searchText = Request["TxtSearch"];
}
Related
I have a usercontrol as a header on a page. The usercontrol has sign up button which opens a modalpopupextender. The popup open code on sign up link is below:
protected void lnkSignUp_Click(object sender, EventArgs e)
{
mp1.Show();
}
Now I am able to successfully use this usercontrol in my page. The problem is in my page there are other links apart from header which will provide sign up facility. Now I want to use the same signup popup of usercontrol to be opened by clicking on page sign up links. What should I do on page link click. I have tried the below code from page but myMpeModal returns null:
ucHeaderJobseeker uc1 = new ucHeaderJobseeker();
var myMpeModal = (AjaxControlToolkit.ModalPopupExtender)uc1.FindControl("mpeModal");
myMpeModal.Show();
Try doing something like this:
Your Front End (.aspx Page):
<uc1:WebUserControl runat="server" ID="myUserControl" />
Your UserControl Should look like this:
<ajax:ModalPopupExtender ID="mpeModal" runat="server" BackgroundCssClass="modalBackground"
PopupControlID="pnlShowMe" TargetControlID="btnClickMe" CancelControlID="btnClose">
</ajax:ModalPopupExtender>
<asp:Button ID="btnClickMe" runat="server" Text="Click Me" OnClick="btnClickMe_Click" />
<asp:Panel ID="pnlShowMe" runat="server" CssClass="ModalPanel">
</asp:Panel>
Your Code Behind (.aspx.cs):
var myMpeModal = (AjaxControlToolkit.ModalPopupExtender)myUserControl.FindControl("mpeModal");
myMpeModal.Show();
Here in the above code, "myUserControl" is your user control, "mpeModal" is your ModalPopupExtender id.
Use the above code where ever you want in your page to show the popup.
I am unfamiliar with C#/ASP/.NET (learning as I go), so it is very likely that I am going about this in an inferior way, in addition to the problems with my current way of doing this. I will try to be as clear as possible (maybe overly so...).
I have a form with a textbox to take in a list of server hostnames, separated by line returns. Upon pressing the submit button, it uses PostBackUrl="~/btil_Info.aspx". In btil_info.aspx.cs codebehind, I get the info from said textbox (hostnames_tb) from the previous form in Page_Load() using:
string hostnames = ((TextBox)Page.PreviousPage.FindControl("hostnames_tb")).Text;
Within Page_Load(), I loop through this list of hostnames and build a form containing several fields for each host (a few textboxes and a dropdown menu for each host). After filling out these fields, upon pressing the Submit button here which calls Submit_Click() in the same codebehind, the page appears to reload because Page_Load is called again before Submit_Click() is executed. In doing so, the the filled form data is lost and the list of hostnames is also lost.
At an earlier stage in development, I had this all on the very first form page, and the submit button didn't call PostBackURL, it simply called Submit_Click(), which worked perfectly fine. The page didn't reload, the form stayed on the page, and I didn't lose data. But as it is now, the button works the same way. The only difference (that I see) being that this troublesome page is reached via the previous form calling PostBackURL.
I don't believe there's any point where anything should be requesting a page refresh/reload. I don't actually care if the page refreshes/reloads as long as the form data is posted and as long as the list of hostnames from the previous form is also retrievable. I just need to be able to access the form data and list of hostnames so that I can pass it to another method of mine that will update a SharePoint list (the code for that part is already working, though). I tried making the hostnames string a class variable in the 2nd codebehind (btil_Info.aspx.cs) and setting it as soon as soon as the page loads if it is empty, but apparently it does not persist over the page reload and is set back to null.
Here's some snippets of code that I hope will be of some help:
First page, add_BTIL.aspx:
<form id="form1" runat="server">
<div>
<asp:TextBox ID="hostnames_tb" runat="server" Rows="20" TextMode="MultiLine" Width="225px"></asp:TextBox>
<br />
<asp:Button ID="Submit" runat="server" PostBackUrl="~/btil_Info.aspx" Text="Submit" />
<br />
<asp:Literal ID="result" runat="server"></asp:Literal>
<br /><br />
<textarea id="hostnames_textarea" cols="25" rows="20" name="hostnames" title="Hostnames"></textarea></div>
</form>
First page codebehind, add_BTIL.aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Submit_Click(object sender, EventArgs e)
{
string hostnames = hostnames_tb.Text;
/*
* Etc.
*/
}
Second page, btil_Info.aspx:
<form id="hosts_form" runat="server">
<p>
<asp:Button ID="Submit" runat="server" Text="Submit" OnClick="Submit_Click" Height="26px" UseSubmitBehavior="False" /><br />
<asp:Literal ID="result" runat="server"></asp:Literal><br />
</p>
<br />
</form>
^ In this form, I read somewhere in my searches that UseSubmitBehavior="False" would prevent a page reload, but it did not help. I didn't use it in my earlier version anyway, and did not have this issue.
Second page codebehind, btil_Info.aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
string hostnames = ((TextBox)Page.PreviousPage.FindControl("hostnames_tb")).Text;
// etc.....
}
protected void Submit_Click(object sender, EventArgs e)
{
// etc.....
}
Many thanks for any help!
If you're posting from add_BTIL.aspx to btil_Info.aspx then the Submit_Click function in file add_BTIL.aspx.cs will never be called.
Based on the limited markup you've given us for the btil_Info.aspx page...
If you aren't rendering your host names list inside of server controls inside the form id="hosts_form" then when you hit the submit button nothing is going to be posted to your Submit_Click function.
Personally I don't like post from one page to another so here are some suggestions below.
If you want to do a multi-page wizard then you might consider the asp.net wizard control.
Or if you want to keep it simple, use two asp Panel controls and show the first one, post the data, and then hide the first and show the second one.
I have two asp:Labels, the first of which is replaced with a few buttons and the second with a list of items.
I want to click on the buttons to filter the items.
The contents of the buttons are added programmatically by replacing the text with html and works fine.
asp:
<form id="form1" runat="server">
<asp:Label id="filters" runat="server" Text="Filters here"/>
<asp:Label id="itemList" runat="server" Text="List of items here"/>
</form>
resultant html of filters label:
<input type="submit" onclientclick="Load_Items(0)" runat="server" value="First"/>
<input type="submit" onclientclick="Load_Items(1)" runat="server" value="Second"/>
<input type="submit" onclientclick="Load_Items(2)" runat="server" value="Third"/>
relevant c#:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Load_Items(0);
}
}
public void Load_Items(int filterType)
{
//code to load items (pseudo below)
for each row in list
if filterType = itemType
build string
replace second label with string
}
On page load everything happens just as I want it to with the contents being filtered by the first item (hence Load_Items(0)), and if I manually change the 0 to another number in Page_Load, it filters by the other types, but if I click the buttons which are programmatically added, nothing happens other than what looks like the page refreshing.
I know the post back check is working by adding a text replacement before and inside it.
I've also added an asp:button to make sure it's not something to do with the way the buttons are added as below (with some extra things recommended from searches):
<asp:Button runat="server" CausesValidation="False" onclientclick="Load_Items(2); return false;" text="Submit" />
So what could be the issue?
The OnClientClick property specifies the javascript to run in the browser when the button is clicked. Since you probably don't have a javascript function called Load_Items, this will generate a script error, and the button will then cause the form to post back.
The server-side Click event will fire on the server, but doesn't allow you to pass a parameter. You will only get the button instance and an empty EventArgs instance.
You might be better off using the Command event, combined with the CommandArgument property.
<asp:Button runat="server" CommandArgument="2" OnCommand="Load_Items" ...
The event handler would use the CommandArgument property of the CommandEventArgs to access the argument from the clicked button:
protected void Load_Items(object sender, CommandEventArgs e)
{
Load_Items(Convert.ToInt32(e.CommandArgument));
}
Well, that's the common problem which I think every asp.net developer deals some time. The common part of it, that asp.net event system doesn't work, as windows forms.
Page object, and all controls on that page, have lifecycle events, that are triggered during any request, even when it's from update panel.
As you create those controls by code, you have to keep in mind, that all events for those controls should work as part of Page object. That's why you have to create those object in Page_Init event, before all other control's event would be triggered.
Please also keep in mind that you have to create those controls as asp.net objects:
var btn = new Button();
But not by simply adding html markup. And you have to recreate them on each request, following that one, when they were created.
Please take a look on my another answer.
On my page a I have a few textboxes, AJAX tabpanel and a gridview.
In the Page_Load event, textboxes are filled in; gridview is populated and so on.
My gridview has a button:
<Columns>
<ItemTemplate>
<asp:Button ID="btnRedirect"
Text="Click me"
CommandArgument='<%#Eval("BkId_ZW")%>'
OnClick="DoRedirect"
runat="server" />
</ItemTemplate>
...
</Columns>
and the code behind it looks like:
protected void DoRedirect(object sender, EventArgs e)
{
Button theButton = sender as Button;
string url ="http://../profile/" + theButton.CommandArgument;
Response.Write("<script>window.open('" + url + "');</script>");
}
After button is pressed a new window is open (everything works as expected) but the main page loose values and formatting of the textboxes.
What is going on? How to fix it?
Response.Write() is not favored for this purpose. Since you are adding to the response in the middle of the ASP.net page lifecycle, you are changing the page output. If you view source, you might find your <script>window.open...</script> line in some awkward place.
Therefore, use Page.ClientScript.RegisterStartUpScript() instead.
I set the value of the controls when the DOM loads. I have this super simple code on aspx page:
<script type="text/javascript">
$(document).ready(function () {
$('#<%=textBox2.ClientID %>').val($('#<%=textBox1.ClientID %>').val());
$('#<%=lblVal.ClientID %>').html($('#<%=textBox1.ClientID %>').val());
});
</script>
<asp:TextBox runat="server" ID="textBox1" Text="Test data" />
<asp:TextBox runat="server" ID="textBox2" />
<asp:Label runat="server" ID="lblVal" Text="Old Data" />
<asp:Button runat="server" Text="Click Me" onclick="Unnamed1_Click" />
In my button click event handler I have this code:
protected void Unnamed1_Click(object sender, EventArgs e)
{
Debug.Write(textBox2.Text);
Debug.Write(lblVal.Text);
}
The thing that came shocking to me lblVal has it's old value. Setting value in javascript doesn't really have any effect on label whereas the textbox2's data is reflected on the server. Is the intended behavior of textbox and label? This came as a bit of surprise to me because I never came across this thing previously.
The label will get rendered to an HTML label tag, not a form field. Therefore it does not have a value that is posted when the form is submitted, and the value you get restored in the postback is from ViewState. Form fields (e.g. a ASP TextBox which becomes an input) will post their value and this will override the value in viewstate.
You could get around this by instead having a hidden input which you update with javascript, and then in your postback update the label with the contents of that instead.
An asp:Label control renders to a span, and is therefore not a form element, and its changed contents will not be posted to the server.