In my application i am using one Fileupload controller ,one dropdown and One button, Here first i am selecting one .doc file using fileupload controller, then i am selecting Dropdown value, when i am clicking button, it checks dropdown value is > 0 or not,
if (ddlstype.SelectedValue != "0")
if the ddlstype value is equals 0, then it shows an Error message in label.
Here the dropdown have AutoPostBack,code follows,
<asp:DropDownList ID="ddlstype" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlstype_SelectedIndexChanged" Width="165px">
Here my problem is if the page is AutoPostBack, then the file upload control become null, how can i maintain the file in fileupload controler while AutoPostBack?
Here you store file path in session on page load. This way you not need to use update panel
protected void Page_Load(object sender, EventArgs e)
{
if (this.IsPostBack)
{
if (Session["FileUpload1"] == null && Request.Files["FileUpload1"].ContentLength > 0)
{
Session["FileUpload1"] = Request.Files["FileUpload1"];
ImageErrorLabel.Text = Path.GetFileName(Request.Files["FileUpload1"].FileName);
HttpPostedFile file = (HttpPostedFile)Session["FileUpload1"];
}
else if (Session["FileUpload1"] != null && (Request.Files["FileUpload1"].ContentLength == 0))
{
HttpPostedFile file = (HttpPostedFile)Session["FileUpload1"];
ImageErrorLabel.Text = Path.GetFileName(file.FileName);
}
else if (Request.Files["FileUpload1"].ContentLength > 0)
{
Session["FileUpload1"] = Request.Files["FileUpload1"];
ImageErrorLabel.Text = Path.GetFileName(Request.Files["FileUpload1"].FileName);
}
}
}
And get it on ddlstype_SelectedIndexChanged
protected void ddlstype_SelectedIndexChanged(object sender, EventArgs e)
{
HttpPostedFile FileUpload1 = Request.Files["FileUpload1"];
Session["FileUpload1"] = FileUpload1;
}
Related
I have cookies and viewstate in below control .
This ajax Control is used to upload multiple file images.
protected void OnUploadComplete(object sender, AjaxControlToolkit.AjaxFileUploadEventArgs e)
{
int userid = 25;
DAL_Cart objdalcart = new DAL_Cart();
if (Viewstate["Imagestringname"] == null)
{
objdalcart.InsertTempImage(userid, ImageName, 1);
}
else
{
objdalcart.InsertTempImage(userid, ImageName, 0);
}
Response.Cookies["JewelleryUserCookiesUserId"].Value = Convert.ToString(userid);
Response.Cookies["JewelleryUserCookiesUserId"].Expires = DateTime.Now.AddYears(1);
Viewstate["Imagestringname"] = ImageName + ",";
}
The issue is when I try to retrive view state value or Cookies value on different click event of button in same page I am not able to retrive the value
protected void lnkcheckout_Click(object sender, EventArgs e)
{
if (Request.Cookies["JewelleryUserCookiesUserId"] == null || Request.Cookies["JewelleryUserCookiesUserId"].Value == "")
{
}
if (Viewstate["Imagestringname"] != null)
{}
}
For both the case it is going in if condition. for viewstate I have placed Enableviewstate=true on master page .Any idea why?
Review
Want ajax multiple file upload on my button click event
var c = new HttpCookie("JewelleryUserCookiesUserId");
c.Value = Convert.ToString(userid);
c.Expires = DateTime.Now.AddYears(1);
Response.Cookies.Add(c);
Just note: this is insecure. the client can manipualte the cookie...
I have an ASP:TreeView that I want to show on icon based upon the file extension. Here is my current code. I have also tried ondatabound using the same code but that does not work neither.
protected void MyTree_TreeNodeExpanded(object sender, TreeNodeEventArgs e)
{
string fileExt = Path.GetExtension(e.Node.Selected.ToString());
if (fileExt == ".pdf")
{
MyTree.LeafNodeStyle.ImageUrl = "/Images/icons/pdf_icon.png";
}
else
{
MyTree.LeafNodeStyle.ImageUrl = "/Images/icons/document_icon.png";
}
}
The script above does not loop through the file structure. In the example below, the pdf file should have the pdf icon and the rest have the document icon.
Server.MapPath specifies the relative or virtual path to map to a physical, but ImageUrl value must be absolute url or relative url.
You need to
Replace
MyTree.LeafNodeStyle.ImageUrl = Server.MapPath("~/Images/icons/pdf_icon.png");
with
MyTree.LeafNodeStyle.ImageUrl = "/Images/icons/pdf_icon.png";
Edit
-e.Node return the expanded Node that is "Nursing" node in your example, but you need to loop through e.Node.ChildNodes.
- e.Node.Selected Returns boolean value you should use e.Node.Text to get node text.
- MyTree.LeafNodeStyle.ImageUrl changes all leafs style in tree so you need to change ImageUrl for every leaf.
this code must do the job.
protected void MyTree_TreeNodeExpanded(object sender, TreeNodeEventArgs e)
{
for (int i = 0; i < e.Node.ChildNodes.Count; i++)
{
if (e.Node.ChildNodes[i].ChildNodes.Count != 0)
continue;
string fileExt = Path.GetExtension(e.Node.ChildNodes[i].Text);
if (fileExt == ".pdf")
{
e.Node.ChildNodes[i].ImageUrl = "/Images/icons/pdf_icon.png";
}
else
{
e.Node.ChildNodes[i].ImageUrl = "/Images/icons/document_icon.png";
}
}
}
I need to update the imageURL of an imageButton of given ID in C#?
I tried using: FindControl(), but I got a null value as result
In ASPX Page
<asp:ImageButton ID="imgBtn1" runat="server" ImageUrl="Cards/1.gif" onClick="Image_Click"/>
In C# code
ImageButton imgButton = (ImageButton)FindControl("imgBtn1");
I am getting imgButton = null
I create a button Reset which calls the method btnReset_Click and in this method I need to find the imageButton:
protected void btnReset_Click(object sender, EventArgs e)
{
ImageButton imgButton = (ImageButton)FindControl("imgBtn1");
}
You shouldnt need to use FindControl("imgBtn1"), as the designer in visual studio should have generated the necessary object for you to use.
Can you access it by just typing:
ImageButton imgButton = imgBtn1;
Perhaps?
EDIT
Try the following code and see if it works for you :)
protected void Page_Load(object sender, EventArgs e)
{
ImageButton imgButton = FindControl<ImageButton>("imgBtn1", this);
}
public T FindControl<T>(string name, Control current) where T : System.Web.UI.Control
{
if (current.ID == name && current is T) return (T)current;
foreach (Control control in current.Controls)
{
if (control.ID == name && control is T)
{
return (T)control;
}
foreach (Control child in control.Controls)
{
var ctrl = FindControl<T>(name, child);
if (ctrl != null && ctrl.ID == name) return ctrl;
}
}
return default(T);
}
in linkbtn
protected void lnkBtnSun_Click(object sender, EventArgs e)
{
Session["employeeName"] = txt_EmpName.Text;
Session["Projectstaus"] = ddownList.SelectedValue;
Session["Startdate"] = txt_StartDate.Text;
Session["EndDate"] = txt_EndDate.Text;
Session["lblsun"] = lbl_sun.Text;
Session["lblmon"] = lbl_Mon.Text;
}
in timesheet.aspx
protected void Page_Load(object sender, EventArgs e)
{
string employeeName = (Session["employeeName"] != null) ? Session["employeeName"] : "";//cursor moves from textbox dropdown data is disappering
string projectStatus = (Session["Projextstaus"] != null) ? Session["Projextstaus"] : "";//maintains value when retun back from another webform
string startDate = (Session["Startdate"] != null) ? Session["Startdate"] : "";
string endDate = (Session["EndDate"] != null) ? Session["EndDate"] : "";//String was not recognized as a valid DateTime.
string lblsun = (Session["lblsun"] != null) ? Session["lblsun"] : "";
string lblmon = (Session["lblmon"] != null) ? Session["lblmon"] : "";
}
maintain state of previous page when I click on linkbutton
I have two webforms in on webform I contross when i click on linkbutton it goes to nextpage ther if im done while retunrsn to main page im loosing data in mainpage ,I tried session but not worked.
txt_EmpName.Text = (Session["employeeName"] != null) ? Session["employeeName"].ToString() : "";
ddownList.SelectedValue = (Session["Projectstaus"] != null) ? Session["Projectstaus"].ToString() : "";
Session is the typical solution in these scenarios. Are you writing those properties to the Session object? Otherwise, they will always be null and you'll see the behavior you're describing.
I assign values from a queryString to these textboxes and that works fine , but whenever I edit the text in one of them and try to get the edited data to be saved in XML node, I can't
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString != null)
{
TextBox_firstname.Text = Request.QueryString["column1"];
TextBox_lastname.Text = Request.QueryString["column2"];
}
else
{
}
}
Is there something with this code? It saves the unedited version in the nodes!
public string str_id;
public int id;
id = int.Parse(str_id);
XDocument xdoc = XDocument.Load(filepath);
if (id == 1)
{
var StudentNodeWithID1 = xdoc.Descendants("students")
.Elements("student")
.Where(s => s.Element("id").Value == "1")
.SingleOrDefault();
StudentNodeWithID1.Element("first_name").Value = TextBox_firstname.Text;
StudentNodeWithID1.Element("last_name").Value = TextBox_lastname.Text;
}
Page_Load is fired on every load (on postback as well as on initial load). Your code is currently defaulting those values from Request.QueryString on every load, before your event handler tries to save it.
Do this instead:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack && Request.QueryString != null)
{
TextBox_firstname.Text = Request.QueryString["column1"];
TextBox_lastname.Text = Request.QueryString["column2"];
}
else
{
}
}
If you are submitting the edited textboxes, then you will need to wrap the code in your Pageload with IsPostback check to ensure that values do not get reset to their originals.