I am new to ASP.net. I downloaded one responsive page template from this link. For my convenience I changed this HTML code to asp.net web form(i.e .html to .aspx) I added a button in that page and I generated click event. When I place a breakpoint in the .cs code and when I click this button in the browser the event is not handled. Even I tried with link button and other controls. I wrote some code in page_load event. It works perfectly. How to solve this issue. Any problem with the downloaded template or can't I handle events in such type of layout. Please go through the below code
Even I tried placing outside the html list control. Its not working. Please go through the link for the details of template here
Issue Placed Menu but event is not working. I tried using other control and its working..
<asp:Menu ID="mini" OnMenuItemClick="mini_MenuItemClick" runat="server" Orientation="Horizontal">
<Items> <asp:MenuItem Text="Item">
<asp:MenuItem Text="sdaad" Value="1">
<asp:MenuItem Text="MenuSub1" Value="MenuSub1" ></asp:MenuItem>
<asp:MenuItem Text="MenuSub2" Value="MenuSub2" ></asp:MenuItem>
</asp:MenuItem>
<asp:MenuItem Text="Item" Value="Item">
<asp:MenuItem Text="MenuSub" Value="MenuSub"></asp:MenuItem>
</asp:MenuItem>
</asp:MenuItem></Items>
</asp:Menu>
I have tried again after download that template and get issue if because of jQuery Validation script, there are three text boxes in bottom so when we are click on our asp.net button then page will first run client side script and client side script will return false so it will not do post-back till validation js will not return true, so to solve asp.net click problem just remove :
<script type="text/javascript" src="js/jqBootstrapValidation.js"></script>
Have you attached ID to your button? Give ID to your Button and try again. As it is server control, it is not so logical to use it without ID... And post your code behind, I need to look at it and then may be I could help you.
One way to check whether the event has been bound to the control, is to go to the design view of the page, select properties for the button, see the section for events and check whether an event has been attached to click. If not double click in the click event section and an event will get attached to your element.
Button.Click event
Why do you want to put that button inside a list control?
Give an ID to your button control.
ID="Button1"
Your Click event should be like this:
protected void Button1_Click(object sender, EventArgs e){
//some code
}
Find and change Categories block to the next:
<div class="categories">
<asp:Button ID="Button1" CssClass="btn btn-primary" runat="server" Text="Button" OnClick="Button1_Click" />
<ul class="cat">
<li>
<ol class="type">
<li>All</li>
<li>Web Design</li>
<li>App Development</li>
<li>Branding</li>
</ol>
</li>
</ul>
<div class="clearfix"></div>
</div>
your ButtonClickListenes is:
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write("<script language='javascript'>alert('Button is working');</script>");
}
Your code behind (full version):
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
MyFunction();
}
protected void MyFunction()
{
SqlConnection con = new SqlConnection("Data Source=DESKTOP-CEUQVES;Initial Catalog=register;Integrated Security=True;Pooling=False");
con.Open();
SqlDataAdapter sda = new SqlDataAdapter("select * from Admintr where projectid=mba ", con);
DataSet ds = new DataSet();
sda.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
GridView1.DataSource = ds;
GridView1.DataBind();
//GridView1.Columns[0].Visible = false;
}
}
protected void Unnamed_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "view")
{
string str = e.CommandArgument.ToString();
Response.ContentType = "image/jpg";
Response.AddHeader("Content-Disposition", "attachment;filename=\"" + str + "\"");
Response.TransmitFile(Server.MapPath(str));
Response.End();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write("<script language='javascript'>alert('Button is working');</script>");
}
Related
I wrote the following code for multiview where I am using gridview and datalist:
<ContentPlaceHolderID="ContentPlaceHolder1">
<div class="alert alert-success" >
<div class="divbtn" style="text-align:right">
<asp:LinkButton ID="gridbtn" runat="server" CssClass="glyphicon glyphicon-th" OnClick="gridbtn_Click"></asp:LinkButton>
<asp:LinkButton ID="listbtn" runat="server" CssClass="glyphicon glyphicon-th-list" OnClick="listbtn_Click"></asp:LinkButton>
</div>
</div>
<asp:MultiView runat="server" ID="multiview" ActiveViewIndex="0">
<asp:View runat="server" ID="gridview">
<uc1:GridControl runat="server" ID="GridControl"/>
</asp:View>
<asp:View runat="server" ID="listview">
<uc1:Listing runat="server" ID="Listing" />
</asp:View>
</asp:MultiView>
</asp:Content>
I am using two link buttons to call their respective views by firing two separate events as follows.
protected void listbtn_Click(object sender, EventArgs e)
{
multiview.ActiveViewIndex = 1;
}
protected void gridbtn_Click(object sender, EventArgs e)
{
multiview.ActiveViewIndex = 0;
}
Suppose, my datalist (Index=1) is active on my page and if there is a post back it should still be showing the datalist but on postback it automatically switches back to grid view (Index=0). I really need help with this!
You can save the index to a session variable and then read it back on post back like this:
To save:
Session["index"] = index.ToString();
Read it on page load like this:
Index = Session["index"];
You will need the session variable to maintain state per user session. If you want to maintain state for the application then you have to use the application variable.
Hi add the following code in your page_load event.
if(!Page.IsPostBack)
{
multiview.ActiveViewIndex=0;
}
i think you are setting the multiview's active index to zero on every post back like follows
protected void Page_Load(object sender, EventArgs e)
{
multiview.ActiveViewIndex=0;
}
this will cause the multiview to set active index as 0 on every post back.To avoid this you have to set it as follows
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
multiview.ActiveViewIndex=0;
}
}
I have a FormView with data(DataSource,DataBind) that I fill with value='<%# Eval("Name") %>' , but after I'm changing the text in TextBox and press update button I see the same value that before, I cant see new value that I have typed.
What I am missing here?
my html
<asp:FormView ID="MainFormTemplate" runat="server">
<ItemTemplate>
<li class="li_result" runat="server">
<div class="col-3">
<input id="txt_Name" runat="server" value='<%# Eval("Name") %>'>
</div>
</li>
</ItemTemplate>
</asp:FormView>
<asp:Button id="btn_Update" runat="server" OnClick="btn_Update_Click" Text="Update" />
Server side
protected void Page_Load(object sender, EventArgs e)
{
using (DB_MikaDataContext data = new DB_MikaDataContext())
{
MainFormTemplate.DataSource = data.File_Projects.Where(x => x.Num_Tik.Equals("12")).ToList();
MainFormTemplate.DataBind();
}
}
public void btn_Update_Click(object sender, EventArgs e)
{
//using System.Web.UI.HtmlControls
HtmlInputText twt = (HtmlInputText)MainFormTemplate.FindControl("txt_Name");
string text = twt.Value;//i see old value ,not new one that i typed in text box
}
In every postback, you are always getting the old value from your database. The solution is check if the page is being rendered for the first time (!IsPostBack) then set your MainFormTemplate's DataSource else if is being loaded in response to a postback (IsPostBack) get the txt_Name's value like this:
HtmlInputText twt;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
using (DB_MikaDataContext data = new DB_MikaDataContext())
{
MainFormTemplate.DataSource = data.File_Projects.Where(x => x.Num_Tik.Equals("12")).ToList();
MainFormTemplate.DataBind();
}
}
else
{
twt = MainFormTemplate.FindControl("txt_Name") as HtmlInputText;
}
}
protected void btn_Update_OnClick(object sender, EventArgs e)
{
string text = twt.Value; // You will get the new value
}
with Page_Load executing every postback, you are always writing value from database (?), and value sent from browser is lost (although still exist in Page.Request.Form member).
In ASP.NET, When a page is submitted, the Page_Load event runs before the button click event. So, the textbox value gets repopulated with its original value before the click event looks at that value.
If this is the situation, then you can wrap the code that assigns the value to the textbox in an if block like this:
if (!IsPostBack)
{
HtmlInputText twt = (HtmlInputText)MainFormTemplate.FindControl("txt_Name");
string text = twt.Value;
}
Hope this helps you.
I am rendering some html in code behind using StringBuilder which includes a button. I am also trying to add a handler to this and I think perhaps not surprisingly it does not fire. Can I include an event handler to a button in this way?
sb.Append("<td>");
sb.Append("<input type='button' runat='server' id='butResetPassword' value='Reset Password' onserverclick='butSendEmail_Click' />");
sb.Append("</td>");
User.InnerHtml = sb.ToString();
this is the eventhandler that appears in the same code behind page
protected void butSendEmail_Click(object sender, EventArgs e)
{
labTester.InnerText = "Thanks for clicking me";
}
I answered a similar question of yours recently.
Unless you have a really good reason to generate markup in the code behind, keep it separate, otherwise stick to classic ASP.
You can do:
Markup:
<asp:Button runat="server" id="butResetPassword" OnClick="butSendEmail_Click" />
Code behind:
protected void butSendEmail_Click(object sender, EventArgs e)
{
//Reset the password here
labTester.InnerText = "Thanks for clicking me";
}
Edit* If all you need to do is display that confirmation message and not interact with any data or controls, then you could bind the button to a js function, change onserverclick to onclick:
<input type='button' runat='server' id='butResetPassword' value='Reset Password' onclick='butSendEmail_Click' />
And then have some js to handle this:
<script type="text/javascript">
function butSendEmail_Click() {
alert("Thanks for clicking me");
//Or set the labels text
var elem = document.getElementById("labTester");
elem.InnerHtml = "Thanks for clicking me";
}
</script>
I'm building a table of data for price quotes (think table of Stock quotes) which needs to be refreshed every 5 secs. Each row has some data about one Stock in several columns and the last column in each row has a LinkButton to see more info about that particular stock. Everything works but the LinkButton. The entire table is nested inside an UpdatePanel which I think is causing the problem. I've seen a fair number of posts on this topic but none that have worked for me.
Here is my .aspx code:
<asp:ScriptManager ID="ScriptManager" runat="server" />
<asp:Timer ID="Timer" OnTick="Timer_Tick" runat="server" Interval="5000" />
<div id="itemList">
<asp:UpdatePanel ID="itemPanel" UpdateMode="Conditional" ChildrenAsTriggers="false" runat="server">
<Triggers><asp:AsyncPostBackTrigger ControlID="Timer" /></Triggers>
<ContentTemplate>
<asp:Panel ID="Panel_ItemList" runat="server" width="100%"></asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
</div>
and my .aspx.cs code:
protected void Page_Load(object sender, EventArgs e)
{
...
if (!Page.IsPostBack)
{
updateItemsTable();
}
}
protected void LinkButton_Click(object sender, CommandEventArgs e)
{
Panel_LoginAlert.Visible = true; // <-- THIS IS NOT FIRING!!
}
protected void Timer_Tick(object sender, EventArgs e)
{
updateItemsTable();
}
protected void updateItemsTable()
{
//... Query my DB
if (rdr.HasRows)
{
Panel_ItemList.Controls.Add(new LiteralControl("<!-- ItemList Panel -->\n"));
while (rdr.Read())
{
LinkButton lb = new LinkButton();
lb.Text = "Item";
lb.ID = "lbItem_" + strDBitemID;
lb.CommandName = strDBitemName;
lb.CommandArgument = strDBitemID;
lb.Command += new CommandEventHandler(LinkButton_Click);
Panel_ItemList.Controls.Add(lb);
}
Panel_ItemList.Controls.Add(new LiteralControl("<!-- END ItemList Panel -->\n"));
}
//...
conn.Close();
}
So the page loads fine and the timer reloads the table fine, but the LinkButtons do not fire the CommandEventHandler. This works fine if I remove the Timer.
Things I've tried:
I tried using Buttons rather than LinkButtons but this didn't help.
I read dozens of posts saying to add an ID to the LinkButton controls, but this didn't help either.
I believe the problem is when your adding the controls. For this to work the server controls need to be added in the Init event, or overriding OnInit(EventArgs).
Instead of explicitly creating the controls you could replace the panel with a repeater. Then bind your results from the database to the reader.
<asp:Repeater ID="TheRepeater" ...>
<ItemTemplate>
<asp:LinkButton onClick="LinkButton_Click" ...bind values to properties here />
</ItemTemplate>
</asp:Repeater>
code behind
TheRepeater.Visible = rdr.HasRows;
TheRepeater.DataSource = rdr;
TheRepeater.DataBind();
That being said, if all you want to do is alter the UI, that could easily be accomplished with jquery.
I believe the problem is in the page life cycle, since you are creating a dynamic control and adding the event after the page_init or page_load, its not getting hooked up correctly to the control, you could try out the following and see if it works:
Add page init:
protected void Page_Init(object sender, EventArgs e)
{
updateItemsTable();
}
and change the timer tick event to:
protected void Timer_Tick(object sender, EventArgs e)
{
itemPanel.Update();
}
and that should do the trick.
Hope this is of help.
Cheers.
You need to add postback trigger as following:
<asp:PostBackTrigger ControlID="SearchBrn"/>
So I need to add some link buttons to each cell of a asp:calendar control dynamically based on data in the database. I wondering what the best way to do this is so that the the link buttons will be wired up to their events on postbacks (as to my knowledge creating the link buttons on the Day Render event and adding them there will occur after the LinkButton events would have fired).
Does anyone have a nice way to handle this?
EDIT: This is a hack but it works, you will have to get crafty with the event naming...etc
Markup:
<asp:Calendar id="calendar1"
OnDayRender="DayRender"
runat="server">
<asp:LinkButton ID="LinkButton1" style="display:none;" runat="server"
onclick="LinkButton1_Click">LinkButton</asp:LinkButton>
Code-Behind:
protected void DayRender(Object source, DayRenderEventArgs e)
{
LinkButton lb = new LinkButton();
lb.ID = "LinkButton1";
//set all your props
lb.Attributes.Add("href",
"javascript:__doPostBack('" + Calendar1.UniqueID + "$" + lb.ClientID +"','')");
e.Cell.Controls.Add(lb);
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
//do something
}