I have a simple CalendarExtender (from AjaxControlToolkit) attached to a textbox.
<asp:TextBox ID="StartDateText" runat="server" MaxLength="10" Width="70px" AutoPostBack="True" OnTextChanged="StartDateText_TextChanged" />
<asp:ImageButton ID="ImageCalendarStartDate" runat="server" ImageUrl="~/images/Calendar_scheduleHS.png" AlternateText="Click to show calendar" />
<asp:CalendarExtender ID="StartDateCalendarExtender" runat="server" TargetControlID="StartDateText" PopupButtonID="ImageCalendarStartDate" />
In order to control user input, I have the AutoPostBack set to True on the textbox, as well as a function on the TextChanged event (although TextChanged isn't the issue here).
In Page_Load, I have:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
StartDateCalendarExtender.SelectedDate = DateTime.Now.AddDays(-1);
}
}
On opening the page, Page_Load sets the date, but the AutoPostBack triggers a postback right after Page_Load, calling it again with IsPostBack set to true.
Is there a server-side way to prevent this postback?
I tried setting the AutoPostBack property to false, changing the SelectedDate, and setting it back to true, but it keeps firing a postback.
The reason is that because you give the date on the extender, then the extender add it to the text box, then the text box trigger the post back.
How about try to set the text at the TextBox at the first place.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// remove that
// StartDateCalendarExtender.SelectedDate = DateTime.Now;
// and direct set it to the text box.
StartDateText.Text = DateTime.Now;
}
}
Maybe you need to format the DateTime the way you want it.
Related
Here is my DataGridView
<asp:GridView ID="gvPredstave" runat="server" CssClass="gridview"
AlternatingRowStyle-CssClass="even" AutoGenerateColumns="True">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnRezervisi" runat="server" Text="Rezervisi" onclick="Button1_Click" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
This is my code for click on button in GridView
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("AktivneRezervacije.aspx?korisnicko_ime=" + korisnicko_ime);
conn.Close();
}
When i click on button i got this error in browser:
Invalid postback or callback argument. Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%# Page EnableEventValidation="true" %> in a page. For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.
Does anybody know the reason why is this happening.
I tried with EnableEventValidation="false" but it doesn't work.
I recreated the error using your gridView and binding it. The answer lies on the Page_Load event. If you have it like this:
protected void Page_Load(object sender, EventArgs e)
{
bindGridView(); //code to bind the GridView
}
You will get the exception. Change it to:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bindGridView();
}
}
and you should not get the error anymore.
if the page is in postback,
you have two ways to make it work
either add a postback event on your gridview
or control in on page load.
Regarding to the conn.close();
close first the connection after redirecting it to another page.
happy coding.
I have a LinkButton in aspx page.
<asp:TextBox ID="textBoxNote" runat="server" />
<asp:LinkButton ID="linkButtonUpdateNote" Text="Update" OnClick="ButtonUpdateNoteClicked" runat="server" />
the click event handler has the following code
protected void ButtonUpdateNoteClicked(object sender, EventArgs e)
{
var note = textBoxNote.Text;
}
On Postback textBoxNote.Text is empty. I don't get the posted value. How to get the value?
It seems like you are possibly resetting the value in your Page_Load.
Check that you are using IsPostback check in the Page_Load function. see - http://msdn.microsoft.com/en-us/library/system.web.ui.page.ispostback.aspx
private void Page_Load()
{
if (!IsPostBack)
{
DoThisOnce();
}
DoThisOnEachPostback();
}
I have a listbox in asp.net
<asp:ListBox ID="ListBox1" class="listItem" runat="server"
onselectedindexchanged="ListBox1_SelectedIndexChanged">
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
</asp:ListBox>
I want to trigger a function when somebody changes it values...
code which i tried is
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
Label6.Text = Label6.Text + "hello";
}
What's wrong here..
By setting the autopostback as true, my problem is solved but the page is reloaded everytime i change its value which i dnt want as large amount of data get reloaded on every autopostback(bcoz of images on the page).Even i put the following code in page load
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
}
}
but it didnt work out...
The problem here is that you expect that the SelectedIndexChanged event is triggered immediately. But by default the ListControl's AutoPostBack property is set to false.
true if a postback to the server automatically occurs whenever the
user changes the selection of the list; otherwise, false. The default
is false.
So set it to true:
<asp:ListBox ID="ListBox1" AutoPostBack="true" class="listItem" runat="server"
onselectedindexchanged="ListBox1_SelectedIndexChanged">
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
</asp:ListBox>
AutoPostBack="true" is missing add it on ListBox1
I have a legacy asp.net 3.5 application. I need to bind a filed to CssClass so that i can utilize it via jquery.
Basically, in the datagrid, there are 2 buttons. Button one is visible and button two is not visible. On click of button one, i want to perform action and then make button two visible and hide button one. How can i do this? I just need a kick in the right direction...
<asp:LinkButton ID="lnkDelete" runat="server"
ToolTip="Delete Order <%# DataBinder.Eval(Container.DataItem, "TransID")%>"
OnClientClick="return DeleteOrder();"
OnClick="OrderDelete" CommandArgument='<%# Eval("TransID")'
CssClass="">
<asp:Image ID="Image1" runat="server" ImageUrl="~/images/icons/delete.gif"
BorderStyle="None" />
</asp:LinkButton>
My current binding inside the tooltip results in an error, "the server tag is not well formed".
On the code behind OrderDelete, i can disable the delete link, but how can i make the other button visible?
//delete indivisual order
protected void OrderDelete(object sender, EventArgs e)
{
string transactionID = String.Empty;
LinkButton lnkDelete = (LinkButton)sender;
if (lnkDelete != null)
transactionID = lnkDelete.CommandArgument;
if (!String.IsNullOrEmpty(transactionID))
{
//do delete
}
//refresh results
}
For the server tag not well-formed error, try something like this:
ToolTip='<%# String.Format("Delete Order {0}", DataBinder.Eval(Container.DataItem, "TransID")%>'
For the second part of your question, a little more of your code might help to give you a more specific answer, but in lieu of that, if you know which row of the DataGrid you're in, you should be able to do a FindControl in that row for the second button and make it visible.
Update
You might try setting the tooltip in the codebehind, using the RowDataBound event. Something like this:
protected void gv1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton btn = e.Row.FindContorl("lnkDelete") as LinkButton;
// You'll need to retrieve the values you want to dynamically populate
// the ToolTip with from other controls in the row;
// I don't know if you'd be able to use the DataSource or not, but you might.
btn.ToolTip = "Delete Order ";
}
}
I have a LinkButton inside a GridView (via an TemplateField). No matter what I try, the LinkButton will not invoke its event handler. I have tried both:
A traditional event handler ("OnClick")
A OnRowCommand event handler at the GridView level.
In both cases, I've debugged and it doesn't even catch the event handler.
If I move the LinkButton out on the page (so it's not in the GridView), it works fine, so I know the syntax is right.
Here is the "traditional" method:
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton Text="Cancel" ID="DeleteButton" CausesValidation="false" OnClick="CancelThis" runat="server" />
</ItemTemplate>
<asp:TemplateField>
What's interesting is if I remove the "CancelThis" method from the code behind, it throws an error. So I know it's aware of its event handler, because it looks for it when it compiles.
Here is the RowCommand method:
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton Text="Cancel" ID="DeleteButton" CausesValidation="false" CommandName="CancelThis" runat="server" />
</ItemTemplate>
<asp:TemplateField>
In this case, the GridView has:
OnRowCommand="GridView_RowCommand"
It postsback, but never hints at raising the event.
Any idea what I'm missing here?
How are you binding your GridView? Are you using a datasource control? If you are binding manually during Page_Load, it's possible that since the grid is binding every round trip, the event handler isn't catching properly. If this is the case, you may want to try something like:
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
//do binding
}
}
Can you post sample binding code to go with your markup?
If you really want to force the issue, you could hook into the RowDataBound event on the Grid, find the button manually and add the handler in the code behind. Something like:
markup snippet:
<asp:GridView ID="gvTest" runat="server" OnRowDataBound="gvTest_RowDataBound" />
code behind:
protected void gvTest_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
//find button in this row
LinkButton button = e.Row.FindControl("DeleteButton") as button;
if(button != null)
{
button.Click += new EventHandler("DeleteButton_Click");
}
}
}
protected void DeleteButton_Click(object sender, EventArgs e)
{
LinkButton button = (LinkButton)sender;
// do as needed based on button.
}
I'm not sure what the purpose of the button is, but assuming it is a row delete button, you may not want to take this approach as in the event handler, you don't have direct access to the row in question, like you would using the RowCommand event.
Is there a reason you're using the Template field? Vs say a ButtonField? If you use a ButtonField, then you can hook into the RowCommand event.
markup snippet:
<asp:GridView ID="gvTest" runat="server" OnRowCommand="gvTest_RowCommand">
<columns>
<asp:buttonfield buttontype="Link" commandname="Delete" text="Delete"/>
....
</columns>
</asp:GridView>
code behind:
protected void gvTest_RowCommand(object sender, GridViewCommandEventArgs e)
{
if(e.CommandName == "Delete")
{
//take action as needed on this row, for example
int rowIndex = Convert.ToInt32(e.CommandArgument);
GridViewRow currentRow = (sender as GridView).Rows[rowIndex];
//do something against the row...
}
}
You might want to consult MSDN docs on some of these topics:
RowCommandEvent
ButtonField class
EDIT:
To answer your question on the ButtonField - yes I don't see why you couldn't still deal with a buttonfield. Here's a snippet to find the buttonfield during row data bound and hide it (untested but I think would work...)
protected void gvTest_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//let's assume your buttonfield is in column 1
// (you'd know this based on your markup...)
DataControlFieldCell cell = e.Row.Cells[1] as DataControlFieldCell;
if(cell != null)
{
ButtonField field = cell.ContainingField as ButtonField;
//based on your criteria, show or hide the button
field.Visible = false;
//or
field.Visible = true;
}
}
}
Is viewstate turned on on your GridView? This has caught me out numerous times.
<button onclick="window.open('<%#Eval("ReportLinks")%>', '_blank');" title='<%#Eval("ReportLinks")%>'> Link</button>