<asp:TextBox ID="TextBox1" runat="server" Height="30px" Width="700px" placeholder="Enter Title" ></asp:TextBox>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:AjaxFileUpload ID="AjaxUploadImages" runat="server" AllowedFileTypes="jpg,jpeg,png,gif"
onuploadcomplete="AjaxUploadImages_UploadComplete"/>
</ContentTemplate>
</asp:UpdatePanel>
I want to get the text of TextBox1 on the OnUploadComplete event like this-
protected void AjaxUploadImages_UploadComplete(object sender, AjaxControlToolkit.AjaxFileUploadEventArgs e)
{
string path = Server.MapPath("~/images/") + e.FileName;
AjaxUploadImages.SaveAs(path);
string content = TextBox1.Text.ToString();
}
protected void AjaxUploadImages_UploadComplete(object sender,AjaxControlToolkit.AjaxFileUploadEventArgs e)
{
string path = Server.MapPath("~/images/") + e.FileName;
AjaxUploadImages.SaveAs(path);
string content = TextBox1.Text.ToString();
}
Related
I use this code for create timer that want to update a label each second:
timer = new System.Timers.Timer();
timer.Elapsed += new ElapsedEventHandler(OnRefresh_Tick);
timer.Interval = 1000;
The function OnRefresh_Tick call each second but the label text doesn't change.
Edit:
I use the below code for the timer, but when the timer starts, I can't write in the second textbox (txtPassword) and it refreshes and the cursor goes to the first textbox (txtUserName)
.aspx file:
<form id="form1" runat="server">
<br /><br />
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:TextBox ID="txtUserName" runat="server" Height="30px" Width="100px"></asp:TextBox><br /><br />
<asp:TextBox ID="txtPassword" runat="server" Height="30px" Width="100px"></asp:TextBox><br /><br />
<asp:Label runat="server" id="lnkSendVerificationCode"></asp:Label><br /><br />
<asp:Button ID="Button1" runat="server" Text="Start Timer" CssClass="btn" OnClick="Button1_Click"/>
<asp:Timer ID="Timer1" runat="server" Enabled="False" Interval="1000" OnTick="Timer1_Tick"></asp:Timer>
</ContentTemplate>
</asp:UpdatePanel>
</form>
.cs file:
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
}
}
protected void Button1_Click(object sender,EventArgs e)
{
Session["VerificationCodeCounter"] = "20";
Timer1.Enabled = true;
}//Button1_Click
protected void Timer1_Tick(object sender, EventArgs e)
{
int sendVerificationCounter = -1;
try { sendVerificationCounter = int.Parse(Session["VerificationCodeCounter"].ToString()); } catch { }//catch
if (sendVerificationCounter == 1)
{
sendVerificationCounter = -1;
Session["VerificationCodeCounter"] = sendVerificationCounter.ToString();
Timer1.Enabled = false;
}//if
else if (sendVerificationCounter > 0)
{
sendVerificationCounter--;
lnkSendVerificationCode.Text = sendVerificationCounter.ToString();
Session["VerificationCodeCounter"] = sendVerificationCounter.ToString();
}//else if
}//Button1_Click
Try say this:
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<div style="padding:25px">
<h4>enter seconds to count down</h4>
<asp:TextBox ID="txtCount" runat="server"
Height="42px"
Width="80px"
Font-Size="XX-Large"
Text="0"
style="text-align: center">
</asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server" Text="Start Timer" CssClass="btn" OnClick="Button1_Click"/>
<asp:Timer ID="Timer1" runat="server" Enabled="False" Interval="1000" OnTick="Timer1_Tick"></asp:Timer>
<br />
</div>
And code behind would be this:
protected void Button1_Click(object sender, EventArgs e)
{
Timer1.Interval = 1000; // tick our timer each second
Timer1.Enabled = true;
}
protected void Timer1_Tick(object sender, EventArgs e)
{
int MyCount = Convert.ToInt32(txtCount.Text);
if (MyCount > 0 )
{
MyCount = MyCount - 1;
txtCount.Text = MyCount.ToString();
}
else
{
// we are done, stop the timer
Timer1.Enabled = false;
}
}
So, we now see this if we enter 10 (for 10 seconds).
If we hit start timer, then it counts down to 0, and then stops the timer.
Edit: With a up-date panel
So, if we don't want a whole page re-fresh, then we can use this:
<h4>enter seconds to count down</h4>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:TextBox ID="txtCount" runat="server"
Height="42px"
Width="80px"
Font-Size="XX-Large"
Text="0"
style="text-align: center">
</asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server" Text="Start Timer" CssClass="btn" OnClick="Button1_Click"/>
<asp:Timer ID="Timer1" runat="server" Enabled="False" Interval="1000" OnTick="Timer1_Tick"></asp:Timer>
</ContentTemplate>
</asp:UpdatePanel>
Remember, while the whole page will now not re-fresh, do keep in mind that the page life cycle DOES trigger. So, if you have some code in on-page load to setup controls? Then you need this
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// code here to load up grids, and controls
// code here ONLY runs on first time page load
}
}
So, keep in mind that for any up-date panel (button clicks, timer etc.), then the page load event DOES fire each time.
However, that !IsPostback check? The last 100+ web pages I created that loads or sets up ANYTHING on a page will have the above code stub, and check for !IsPostback.
You can't quite even make a working webforms page unless you follow the above rule. What this means is your page can now handle post-backs, and not re-load controls and run the first time setup of such controls.
So, keep in mind, a update panel DOES post-back, and does trigger the page load event. Better said, a update panel, and click of a button (or timer) inside will post-back, but it what we call a partial page post-back.
Edit3: With a label
So, say we want a label - not a text box. Then do this:
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<div style="padding:25px">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Label ID="txtCount" runat="server"
Height="42px"
Width="80px"
Font-Size="XX-Large"
Text="999"
style="text-align: center">
</asp:Label>
<br />
<asp:Button ID="Button1" runat="server" Text="Start Timer" CssClass="btn" OnClick="Button1_Click"/>
<asp:Timer ID="Timer1" runat="server" Enabled="False" Interval="1000" OnTick="Timer1_Tick"></asp:Timer>
</ContentTemplate>
</asp:UpdatePanel>
<br />
</div>
</form>
</body>
And now our code becomes this:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// code here to load up grids, and controls
// code here ONLY runs on first time page load
txtCount.Text = "10";
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Timer1.Interval = 1000; // tick our timer each second
Timer1.Enabled = true;
}
protected void Timer1_Tick(object sender, EventArgs e)
{
int MyCount = Convert.ToInt32(txtCount.Text);
if (MyCount > 0 )
{
MyCount = MyCount - 1;
txtCount.Text = MyCount.ToString();
}
else
{
// we are done, stop the timer
Timer1.Enabled = false;
}
}
And now we see this:
I have a form with a button that validates a required field and then if the validation is ok send me to another page. I have used AJAX with UpdatePanel that shows me a label if the field is required or not.
The problem is that when I press the button and the validation is correct it does not redirect me to another page.
<asp:UpdatePanel ID="upn_Data" runat="server" UpdateMode="Always">
<ContentTemplate>
<div class="row inp-row check-field">
<label for="option" class=" ">Option1 :*</label>
<div class="text-inp with-tooltip">
<asp:DropDownList ID="ddlOption1" runat="server" AutoPostBack="true" onselectedindexchanged="ddlOption1_SelectedIndexChanged">
</asp:DropDownList>
<asp:Label ID="lblRequiredOption1" runat="server" Text="Obligatory field" visible="false" AutoPostBack="true"></asp:Label>
</div>
</div>
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Always">
<ContentTemplate>
<div id = "buttons" class ="buttons">
<asp:Button ID="Button1" runat="server" Text = "Continue" class="continue-btn" onclick="Button1_Click"></asp:Button>
</div>
</ContentTemplate>
</asp:UpdatePanel>
In server:
protected void Button1_Click(object sender, EventArgs e)
{
bool nextPage = true;
if (ddlOption1.SelectedValue == "*")
{
lblRequiredOption1.Visible = true;
nextPage = false;
}
if (nextPage == true) {
Type cstype = this.GetType();
String csName = "RedirectScript";
String script = "window.parent.location = '../Paso2.html'";
ClientScriptManager cs = Page.ClientScript;
cs.RegisterClientScriptBlock(cstype, csName, script.ToString(), true);
}
}
So i'm a newbie in asp.net. I have this college project which need to use update function. I made the update function, its work fine for all columns except for picture's column. Its not updating picture database, and show the previous picture in the gridview. The pictures in the folder already updated but the gridview just show the previous picture (not the updated one). I need your help please thank you :)
this is the gridview aspx code, if update button was clicked, it will redirect to another page.
<div>
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="Images" ItemStyle-Height="50px" ItemStyle-Width="50px" >
<ItemTemplate>
<asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("pic") %>'/>/>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="nama" HeaderText="nama" />
<asp:BoundField DataField="harga" HeaderText="harga" />
<asp:BoundField DataField="stok" HeaderText="stok" />
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnUpdate" runat="server" OnClick="btnUpdate_Click" Text="Update" wID='<%# Eval("Id") %>'/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
this is the Page_Load function
public List<Table> getAll()
{
List<Table> watches = (from x in de.Tables select x).ToList();
return watches;
}
public void loadData()
{
GridView1.DataSource = repo.getAll();
GridView1.DataBind();
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack) loadData();
}
and redirected to update aspx
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<br />
<asp:FileUpload ID="pic" runat="server" />
<br />
<asp:TextBox ID="name" runat="server"></asp:TextBox>
<br />
<asp:TextBox ID="price" runat="server"></asp:TextBox>
<br />
<asp:TextBox ID="stok" runat="server"></asp:TextBox>
<br />
<asp:Button ID="btnUpdate" runat="server" Text="Button" OnClick="btnUpdate_Click" />
This is the update function of btnUpdate in update aspx
protected void btnUpdate_Click(object sender, EventArgs e)
{
string ext = System.IO.Path.GetExtension(pic.FileName);
string nama = name.Text;
int harga = Int32.Parse(price.Text);
int stock = Int32.Parse(stok.Text);
Table update = (Table)Session["updateWatch"];
string pict = "..\\Pictures\\Watch Pictures\\" + update.Id + ext;
bool updateStatus = repo.update(update.Id, pict, nama, harga, stock);
if (updateStatus == false) Label1.Text = "fail";
else
{
string subPic = update.pic.Substring(3);
string path = Server.MapPath("~") + subPic;
string savepath = Server.MapPath("~/Pictures/Watch Pictures/");
System.IO.File.Delete(path);
pic.SaveAs(savepath + update.Id + ext);
Response.Redirect("WebForm1.aspx");
}
}
this is the update function in repository
public bool update(int id, string pic, string nama, int harga, int stok)
{
Table updateW = (from x in de.Tables where x.Id == id select x).FirstOrDefault();
updateW.pic = pic;
updateW.nama = nama;
updateW.harga = harga;
updateW.stok = stok;
de.SaveChanges();
if (updateW == null) return false;
return true;
}
this is the code for gridview datasource
public List<Table> getAll()
{
List<Table> watches = (from x in de.Tables select x).ToList();
return watches;
}
I am following the Managing Browser History asp.net example article but I have a problem with updating the LabelHistoryData label after clicking the browser back button. I placed a breakpoint at
LabelHistoryData.Text = Server.HtmlEncode(e.State["s"]);
but somehow after clicking btnGetInboxList or btnGetSentboxList follow by the browser back button, the LabelHistoryData.Text value is always null. Below is a portion of my code. Someone please kindly advice.
Thanks.
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="True" OnNavigate="OnNavigateHistory" EnableHistory="true" EnableSecureHistoryState="false" />
<asp:UpdatePanel ID="uiupInboxMainGrid" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Label runat="server" ID="LabelHistoryData" />
<asp:Button ID="btnGetMessageContent" runat="server" Text="MsgContent" OnClick="GetMessageContent_Click" />
<asp:Button ID="btnGetInboxList" runat="server" Text="Inbox" OnClick="GetInboxList_Click"/>
<asp:Button ID="btnGetSentboxList" runat="server" Text="OutBox" OnClick="GetSentBoxList_Click" />
</ContentTemplate>
</asp:UpdatePanel>
public void OnNavigateHistory(object sender, HistoryEventArgs e)
{
LabelHistoryData.Text = Server.HtmlEncode(e.State["s"]);
}
protected void GetInboxList_Click(object sender, EventArgs e)
{
LabelHistoryData.Text = ((Button)sender).Text;
ScriptManager.GetCurrent(this).AddHistoryPoint("s", LabelHistoryData.Text, "Entry: " + LabelHistoryData.Text);
}
protected void GetSentBoxList_Click(object sender, EventArgs e)
{
LabelHistoryData.Text = ((Button)sender).Text;
ScriptManager.GetCurrent(this).AddHistoryPoint("s", LabelHistoryData.Text, "Entry: " + LabelHistoryData.Text);
}
protected void GetMessageContent_Click(object sender, EventArgs e)
{
LabelHistoryData.Text = ((Button)sender).Text;
ScriptManager.GetCurrent(this).AddHistoryPoint("s", LabelHistoryData.Text, "Entry: " + LabelHistoryData.Text);
}
Turns out that the UpPanel UpdateMode has to be set to Always.
I posted a number of bugs on Microsoft site and while they were real bugs, MSFT will close it as design [And I figured most people favour MSFT anyways]. Here is one that I am sure they will clasify as by design but to me this is a serious bug.
This is all I have in ASPX page (NET 3.5).
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Submit" /><br />
<asp:Label ID="lblOutput" runat="server" Text="Label"></asp:Label>
Codebehind
protected void Page_Load(object sender, EventArgs e)
{ /* this works */
if (IsPostBack)
{
string txt = string.Empty;
txt = Request.Params["TextBox1"];
lblOutput.Text = "You entered : " + txt;
}
}
protected void Button1_Click(object sender, EventArgs e)
{ /* this does not */
string txt = string.Empty;
txt = Request.Params["TextBox1"];
lblOutput.Text = "You entered : " + txt;
}
Now if you include another simple HTML textbox (Not ASP) like this
<input type="text" id="mytextbox" name="mytextbox" /> // still it below the existing one
txt = Request.Params["mytextbox"]; // change to this line instead of TextBox1
Then it works in both places.
protected void Button1_Click(object sender, EventArgs e)
{ /* Now this works which is weird but it does */
If(IsPostback)
{
string txt = string.Empty;
txt = Request.Params["TextBox1"];
lblOutput.Text = "You entered : " + txt;
}
}
I therefore should close the question.
If you are forced to use the Request object instead normal asp:* controls, use it like this:
txt = Request["TextBox1"];
It will check all the HttpRequest collections.
The QueryString, Form, Cookies, or ServerVariables collection member
specified in the key parameter. If the specified key is not found,
then null is returned.
That is really fundamental stuff you're doing in that example, so I highly doubt it's a bug. From the example, it looks like you're going against the grain:
Markup
<asp:PlaceHolder ID="PlaceHolder1" runat="server">
<asp:TextBox ID="TextBox1" runat="server" />
<asp:TextBox ID="TextBox2" runat="server" />
<asp:TextBox ID="TextBox3" runat="server" />
<asp:TextBox ID="TextBox4" runat="server" />
...
</asp:PlaceHolder>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Submit" /><br />
<asp:Label ID="lblOutput" runat="server" Text="Label"></asp:Label>
Code-behind
protected void Button1_Click(object sender, EventArgs e)
{
foreach (TextBox txtCtrl in PlaceHolder1.Controls.OfType<TextBox>())
{
//append the textbox value to the label
lblOutput.Text += String.Format("{0}<br/>", txtCtrl.Text);
}
}
protected void Button1_Click(object sender, EventArgs e)
{
List<TextBox> txtList = PlaceHolder1.Controls.OfType<TextBox>().ToList();
for (int ctrlIndex = 0; ctrlIndex < txtList.Count; ctrlIndex++)
{
TextBox txtCtrl = txtList.ElementAt(ctrlIndex);
if (txtCtrl != null)
{
lblOutput.Text += String.Format("{0}<br/>", txtCtrl.Text);
}
}
}