I need to navigate to the certain tab when button is clicked.
The button is
<asp:Button ID="ButtonFind" runat="server" Text="Buscar" CssClass="btn btn-primary" UseSubmitBehavior="true" OnClick="ButtonSearch_Click" />
JavaScript
// Javascript to enable link to tab
var url = document.location.toString();
if (url.match('#')) {
$('.nav-tabs a[href="#' + url.split('#')[1] + '"]').tab('show');
}
// Change hash for page-reload
$('.nav-tabs a').on('shown.bs.tab', function (e) {
window.location.hash = e.target.hash;
})
But somehow the method
protected void ButtonSearch_Click(object sender, EventArgs e)
{
// Fill Grid code
}
is replaced with
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// Fill Grid by default
}
}
And finally the grid has no found data and has default data. That's a problem.
I have try to use this
protected void ButtonSearch_Click(object sender, EventArgs e)
{
// Fill Grid code
string url = Request.Url.GetLeftPart(UriPartial.Path) + "#AllWorkOrders";
Response.Redirect(url, true);
}
but there is a
if (!IsPostBack)
{
// Fill Grid by default
}
still ...
I mean I need somehow return user to the correct TAB but Response.Redirect establish some trouble...
Probably there is a way to add #AllWorkOrders part of QueryString to
<asp:Button ID="ButtonFind" runat="server" Text="Buscar" CssClass="btn btn-primary" UseSubmitBehavior="true" OnClick="ButtonSearch_Click" />
but I don't know how to do it.
Any clue?
I found solution that was quite easy to implemenet hahahahaa
I completely forgot about PostBackUrl property!
So the code looks like this now
<asp:Button ID="ButtonFind" runat="server" Text="Buscar" CssClass="btn btn-primary" UseSubmitBehavior="true"
PostBackUrl="~/MyPage.aspx#AllWorkOrders" OnClick="ButtonSearch_Click" />
Related
I have a web form that uses a CustomValidator to check the contents of a textbox. If the textbox's contents are invalid when a "Submit" button is pressed, the error message is displayed properly and the "Submit" operation doesn't happen. When the textbox's contents are corrected and the submit button is pushed, the error disappears and the next page is presented properly. So far so good.
But, if the user then clicks the Back button in browser, the original page is displayed with the error message, even though the textbox still shows the corrected text. Can anyone offer suggestions about what I may be doing wrong?
Code front:
<asp:TextBox runat="server" ID="txtUsername" ValidationGroup="Register" />
<asp:CustomValidator runat="server" ID="valUsername" OnServerValidate="Validate_Username" ValidationGroup="Register" Display="Dynamic" />
<asp:LinkButton runat="server" ID="btnNext" OnClick="btnNext_OnClick" ValidationGroup="Register" CausesValidation="True" />
Code behind:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
valUsername.ErrorMessage = Resources.Resource.RegisterPageValidateUsername;
}
}
protected void btnNext_OnClick(object sender, EventArgs e)
{
Page.Validate("Register");
if (Page.IsValid)
{
//Do stuff
}
}
protected void Validate_Username(object source, ServerValidateEventArgs args)
{
var user = GetUser(txtUsername.Text);
args.IsValid = user == null;
}
I have a textbox filled at the page load. I want to check the value of the textbox is changed or not in the "update" button press. Any solution? TIA.
Well, you could say use client side javascript.
But, you could also do this:
Say this text box:
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server" Text="Done - continue" OnClick="Button1_Click" />
And our code could be this:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// code here to setup and load controls
TextBox1.Text = "Dog";
ViewState["TextBox1"] = "Dog";
}
}
protected void Button1_Click(object sender, EventArgs e)
{
if (TextBox1.Text != (string)ViewState["TextBox1"])
{
// then text box was changed
}
}
I have a textbox and a linkbutton within the EditItemTemplate of my ListView:
<asp:TextBox ID="txt_notes" runat="server" Placeholder='<%# Eval("notes") %>'></asp:TextBox>
<asp:LinkButton ID="btn_update" class="btn btn-sm btn-success" OnClick="btn_update_Click" CommandArgument='<%# Eval("carID") %>' runat="server" >Update</asp:LinkButton>
I have this code:
protected void btn_update_Click(object sender, EventArgs e)
{
var btn_update = (LinkButton)sender;
int ID = System.Convert.ToInt32(btn_update.CommandArgument);
TextBox txt_notes = (TextBox)listview.EditItem.FindControl("txt_notes");
string notes = txt_notes.Text;
}
Now when I set a break point at string notes = txt_notes.Text it says the txt_notes.Text has nothing in it even though I have typed something in the textbox, so it seems that it is being overridden by the ItemDataBound or the PageLoad.
Does anyone know how I should overcome this problem?
It looks like you are binding your listview on page load but not checking if it is on a postback. try the below
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
listview.DataSource = your_data_source;
listview.DataBind();
}
}
I have an button in my aspx web page, which should be 'clicked' in my code behind c# code. The OnClientClick method then call a JS confirm dialog box and so the btn_Click function only get executed when user click 'OK'...
This is my code so far:
Variante 1
aspx:
<asp:Button runat="server" ID="btnConfirm" Text="Confirm" OnClick="btnConfirm_Click" />
aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
// set OnClientClick
btnConfirm.OnClientClick = "confirm('Save in DB?');";
// invoke method
Type t = typeof(System.Web.UI.WebControls.Button);
object[] p = new object[1];
p[0] = EventArgs.Empty;
MethodInfo m = t.GetMethod("OnClick", BindingFlags.NonPublic | BindingFlags.Instance);
m.Invoke(btnConfirm, p);
}
protected void btnConfirm_Click(object sender, EventArgs e)
{
//save something in database
}
Got code samples by: http://forums.asp.net/t/1046550.aspx?How+do+you+raise+a+button+click+event+from+outside+the+button+
I want to call the OnClientClick method, but when I replace 'OnClick' with 'OnClientClick' this error appear:
System.NullReferenceException was unhandled by user code
Message=Object reference not set to an instance of an object.
Edit:
Variante 2:
I tried to rewrite my program like this:
aspx:
<script language="javascript">
function invokeButtonClick() {
document.getElementById("btnConfirm").click();
}
function Validate() {
return confirm('Save in db?');
}
</script>
<asp:Button runat="server" ID="btnConfirm" Text="Confirm" OnClick="btnConfirm_Click" OnClientClick="return Validate();" />
aspx:cs:
protected void Page_Load(object sender, EventArgs e)
{
ScriptManager.RegisterStartupScript(this, GetType(), "InvokeButton", "invokeButtonClick();", true);
}
protected void btnConfirm_Click(object sender, EventArgs e)
{
// save something in database
}
But then the page postback before my btnConfirm_Click function get called...
Thanks,
Michael
If OnClientClick has a return value of true then the codebehind OnClick event will fire. If OnClientClick is returned false then it won't.
If OnClientClick doesn't return anything the codebehind event will fire regardless. So you want..
<asp:Button runat="server" ID="btnConfirm" Text="Confirm" OnClick="btnConfirm_Click" />
<script language="javascript">
function Validate() {
return confirm('Save in db?');
}
</script>
Codebehind:
protected void Page_Load(object sender, EventArgs e)
{
// set OnClientClick
btnConfirm.OnClientClick = "return Validate();";
}
protected void btnConfirm_Click(object sender, EventArgs e)
{
//save something in database
}
UPDATE:
This post just came to my attention, don't know why I didn't include it in my original post but you can use 'OnClientClick' in the aspx tag:
<asp:Button runat="server" ID="btnConfirm" OnClientClick="return Validate();" Text="Confirm" OnClick="btnConfirm_Click" />
<script language="javascript">
function Validate() {
return confirm('Save in db?');
}
</script>
You need to Add both the events on the button click. OnClientClick handles the Client side script code and OnClick handles the server side event calling.
So add both events and there handlers. The NullReferenceException you are getting because of that.
<asp:Button runat="server" ID="btnConfirm" Text="Confirm" OnClick="btnConfirm_Click" OnClientClick="return Validate();" />
and in your javascript code can be
function Validate(){
if (valied())
return true;
return false;
}
100% code behind confirmation! Sometiems its so annyoing to Code behind or Server side confirmation in asp.net when you want to Delete or Update something, I got its solution after a long search as below:
<asp:Button ID="btnSave" runat="server" Text="Save" OnClientClick="return confirm('Are you sure to BLOCK this customer ?')" OnClick="btnSave_Click" />
Now whenever you will click this button it 'll ask to confirm, if you 'll select "NO" page will not post and if you select "Yes" then page will post and your button click event will be fire... like below
Code behind:
protected void Page_Load(object sender, EventArgs e)
{
}
// Before coming here,,,there will be confirmation
protected void btnSave_Click(object sender, EventArgs e)
{
GetCustomerInfo();
}
In my .aspx page I have a 'input' html tag, also an asp button.
<input id="Name" type="text" runat="server" clientidmode="Static" />
<asp:Button Width="100" type="submit" ID="sendOrder" runat="server" OnClick="SubmitForm" Text="Submit" />
On page load, I am filling the value in input tag from code behind, like this:
Name.Value= "X";
But now if I change value of this text box from browser, lets say "Y", and click on Submit button, then I get the old value, but not the new one.
protected void SubmitForm(object sender, EventArgs e)
{
var test= Name.Value; // here I get old value
}
How can I get the altered value?
Make sure you are only setting the value to "X" when its not a postback:
if (!Page.IsPostBack){
Name.Value= "X";
}
Otherwise when clicking the submit button, the Page_Load() event will change the value from "Y" back to "X".
You need to use !IsPostBack on Page_Load shown as below:
protected void Page_Load(object sender, EventArgs e)
{
//it's important to use this, otherwise textbox old value overrides again
if (!IsPostBack)
{
Name.Value= "X";
}
}
Suggestion:
We can use use <input></input> control in asp.net but best practice is to use <asp:TextBox></asp:TextBox> control instead.
Here is the sample example:
HTML
<asp:TextBox ID="Name" runat="server"></asp:TextBox>
<asp:Button Width="100" ID="sendOrder" runat="server" OnClick="SubmitForm"
Text="Submit" />
Code-behind:
protected void Page_Load(object sender, EventArgs e)
{
//it's important to use this, otherwise textbox old value overrides again
if (!IsPostBack)
{
Name.Text = "Some Value";
}
}
protected void SubmitForm(object sender, EventArgs e)
{
var test = Name.Text; //now get new value here..
}
Check for IsPostback in Page_Load so you don't overwrite the values that are submitted!
You don't need all the else portion, just do this
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//code to execute here only when an action is taken by the user
//and not affected by PostBack
}
//these codes should be affected by PostBack
}