Two AjaxFileUpload controls entering the same UploadComplete function - c#

I'm trying to use two Ajaxfileupload controls in the same page, but both enter the same uploadcomplete function and I have no idea why..
(They enter the "AjaxFileUpload1_UploadComplete" function)
here is my aspx part:
<asp:AjaxFileUpload ID="AjaxFileUpload1" runat="server" OnUploadComplete="AjaxFileUpload1_UploadComplete" ThrobberID="myThrobber" MaximumNumberOfFiles="10" AllowedFileTypes="jpg,jpeg"/>
<asp:AjaxFileUpload ID="AjaxFileUpload2" runat="server" OnUploadComplete="AjaxFileUpload1_prof_pic" ThrobberID="myThrobber" MaximumNumberOfFiles="1" AllowedFileTypes="jpg,jpeg"/>
and here is my code behind:
protected void AjaxFileUpload1_UploadComplete(object sender, AjaxControlToolkit.AjaxFileUploadEventArgs e)
{
Directory.CreateDirectory(Server.MapPath("~/Member_Data/" + id + "/images/"));
string filePath = "~/Member_Data/" + id + "/images/";
string path = filePath + e.FileName;
AjaxFileUpload1.SaveAs(Server.MapPath(filePath) + e.FileName);
db1.insert_pic_slide(id, path);
string qstring = "?id=" + id;
//Response.Redirect("profile_layout.aspx" + qstring);
}
protected void AjaxFileUpload1_prof_pic(object sender, AjaxControlToolkit.AjaxFileUploadEventArgs e)
{
Directory.CreateDirectory(Server.MapPath("~/Member_Data/" + id + "/images/"));
string filePath = "~/Member_Data/" + id + "/images/";
string path = filePath + e.FileName;
AjaxFileUpload2.SaveAs(Server.MapPath(filePath) + e.FileName);
db1.insert_pic(id, path);
string qstring = "?id=" + id;
Response.Redirect("profile_layout.aspx" + qstring);
}

I was also faced the same problem, so just I removed the second Ajaxfileupload control , and I upload the files based on the dropdown selected value. I am just using single fileupload control.

Related

C# Textbox on Textmode number not triggering on empty

I am implementing a grid view and putting a textbox in the Headers to be able to filter the data for each column. I have the following code for the ontextchanged trigger for every textbox.
protected void Filter_TextChanged(object sender, EventArgs e)
{
string ColumnName = ((TextBox)sender).Attributes["EventArg"];
string FilterValue = ((TextBox)sender).Text;
if (string.IsNullOrEmpty(FilterValue))
{
EntityDataSource1.Where = "";
}
else
{
int dummy;
if (int.TryParse(FilterValue, out dummy))
{
EntityDataSource1.Where = "it." + ColumnName + " = " + FilterValue;
}else
{
EntityDataSource1.Where = "it." + ColumnName + " Like '%" + FilterValue + "%'";
}
}
}
The aspx is as follows
<asp:TextBox ID="txtCaseNr" runat="server" CssClass="form-control" OnTextChanged="Filter_TextChanged" AutoPostBack="true" TextMode="Number"></asp:TextBox>
Everything works so far except that to prevent text being written at textboxes expecting integers i set the textmode=number. And it is not being triggered when the textbox is emptied to remove the filter.
I hope my problem is clear. Any solutions are welcome.
Thanks,

Image is not stored after upload

Image is not stored into Images folder after upload attempt. What's wrong with my code?
Here is my code:
protected void btnupload_Click(object sender, EventArgs e)
{
if (fileupload1.HasFile)
{
string fileName = fileupload1.FileName.ToString();
string uploadFolderPath = "~/Image/";
string filePath = HttpContext.Current.Server.MapPath(uploadFolderPath);
fileupload1.SaveAs(filePath + "\\" + fileName);
img1.ImageUrl = "~/Image/" + "/" + fileupload1.FileName.ToString();
lblimg_name.Text= fileupload1.FileName.ToString();
}
}
If you are using <asp:FileUpload>, try this:
Or Describe in detail
string strFileName = "fileName";
string strFileType = System.IO.Path.GetExtension(fileupload1.FileName).ToString().ToLower();
fileupload1.SaveAs(Server.MapPath("folderpath" + strFileName + strFileType));
Try this code..
protected void btnupload_Click(object sender, EventArgs e)
{
if (fileupload1.HasFile)
{
string fileName = Path.GetFileName(fileupload1.PostedFile.FileName);
fileupload1.PostedFile.SaveAs(Server.MapPath("~/Image/") + fileName);
}
}
change
img1.ImageUrl = "~/Image/" + "/" + fileupload1.FileName.ToString();
to
img1.ImageUrl = "~/Image/" + fileupload1.FileName;
you have additional "/" in your path

Textbox value in other textbox by clicking button not correct

This program have a link which is fixed and never change. And it contains 5 textboxes. The fixed link is:
<seite>utm_source=<website>_<page>_de&utm_medium=banner&utm_campaign=<kampagne>&utm_content=<format>
Every value in <> should be changed by textbox value. Here you got an image of my little program:
Now my problem is: the first value is correct, but the other values aren't. So for example, if i type in second texbox: "website" it does not only replace <website> with "website". it replaced <website> with System.Windows.Forms.TextBox, Text: website.
My Code I tried:
private void btn_SendAll_Click(object sender, EventArgs e)
{
txt_FinishLink.Text = txt_Site.Text + "utm_source=" + txt_Website + "_" + txt_Page + "_de&utm_medium=banner&utm_campaign=" + txt_Campaign + "&utm_content=" + txt_Format;
}
As pointed out in the comments, the Text property of the TextBox needed to be used:
txt_FinishLink.Text = txt_Site.Text + "utm_source=" + txt_Website.Text + "_" + txt_Page.Text + "_de&utm_medium=banner&utm_campaign=" + txt_Campaign.Text + "&utm_content=" + txt_Format.Text
Text will return the string of characters in the specified TextBox.
private void btn_SendAll_Click(object sender, EventArgs e)
{
txt_FinishLink.Text = txt_Site.Text + "utm_source=" +
txt_Website.Text + "_" +
txt_Page.Text + "_de&utm_medium=banner&utm_campaign=" +
txt_Campaign.Text + "&utm_content=" +
txt_Format.Text;
}
Look at string.Format though, it makes it much easier to see the format of the new url:
private void btn_SendAll_Click(object sender, EventArgs e)
{
txt_FinishLink.Text = string.Format(
"{0}utm_source={1}_{2}_de&utm_medium=banner&utm_campaign={3}&utm_content={4}",
txt_Site.Text, //{0}
txt_Website.Text, //{1} etc.
txt_Page.Text,
txt_Campaign.Text,
txt_Format.Text);
}
Then you may want to consider encoding the text before placing in the URL, see this answer https://stackoverflow.com/a/16894322/360211

Public override string toString, ArrayList and Listbox output in multiplelines?

I've been struggling with this C# problem all night.
I have a override ToString(), which is working fine, and I can put my data out in a ListBox. But as the data is very long, with a bunch of classes, the output becomes long.
I wanted to be able to break my ListBox output into multiplelines.
Here is the override in the class file:
//ToString
public override string ToString()
{
return "Name " + firstName + lastName + ". Nationality " + nationality + ". Lives in " + address + " " + zipCode + " " + city + " " + country + "."//
+ " Height is " + height + " meters. Hair color is " + hairColor + " and eye color is " + eyeColor + ". Specialmarkings: "//
+ specialMark + ". Is associated with " + association + ". Codename is " + codeName + "Photo (filename): " + photo;
}
Here is the index code:
public partial class Index : System.Web.UI.Page
{
static ArrayList personarraylist;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
personarraylist = new ArrayList();
}
}
protected void ButtonCreate_Click(object sender, EventArgs e)
{
//create new object
Person p = new Person(TextBox1FirstName.Text, TextBox2LastName.Text, TextBox3Nation.Text, TextBox4Address.Text, //
TextBox5City.Text, TextBox7Country.Text, //
TextBox10HairColor.Text, TextBox11EyeColor.Text, TextBox12SpecialMark.Text, TextBox13Asso.Text, TextBox14Codename.Text, TextBox15Photo.Text, //
Convert.ToDouble(TextBox9Height.Text), Convert.ToInt32(TextBox6ZipCode.Text), Convert.ToInt32(TextBox8Pass.Text));
//add object to arraylist
personarraylist.Add(p);
}
protected void ButtonShow_Click(object sender, EventArgs e)
{
//clear list box
ListBox1.Items.Clear();
//loop through Arraylist
for (int i = 0; i < personarraylist.Count; i++)
{
ListBox1.Items.Add(personarraylist[i].ToString());
ListBox1.Items.Add("");
TextBox1.Text = "";
}
}
}
Is it possible to break the output in multiplelines in a ListBox?
I was trying to inject some html breaktags in the override return, but these get stripped, yeah this is a webapplication.
Thanks in advance for your time.
PS I am a newbie in C# (Student), so be kind ;)
UPDATE:
Hi again all, thx for the help, I already tried with Environment.Newline and the other solutions, but these seem to be overlooked when displaying the text in a ListBox. I can see the breakpoints in the codebehind, but in the browser the listbox still just keeps it all in one line. So I decided to use a TextBox instead, which breaks the text automaticly and where I point out.
//loop through Arraylist
for (int i = 0; i < personarraylist.Count; i++)
{
TextBox1.Text += personarraylist[i].ToString();
}
Again thx for the help :-)
You can use Environment.NewLine or simply "\n" to create multiple lines of text.
If that doesn't work, you can try using the DataList control:
<asp:DataList id="myDataList" runat="server">
<ItemTemplate>
Line 1
<br />
Line 2
</ItemTemplate>
</asp:DataList>

How to postback server by JavaScript Asp.net

namespace KetBanBonPhuong.Controls.Default
{
public partial class SugFriends : System.Web.UI.UserControl
{
private string Uid;
protected void Page_Load(object sender, EventArgs e)
{
if (Request.Cookies["UId"] != null)
{
string value = Request.Cookies["UId"].Value;
Uid = UserService.GetId_Cookie(value);
}
else
{
Uid = Session["Id"].ToString();
}
LoadListSuggest();
}
private void LoadListSuggest()
{
string str = "";
List<RankByUser> list = new List<RankByUser>();
list = RankByUserService.GetListRank(Uid);
foreach (RankByUser rank in list)
{
str += "<li><div class=\"sug_acc\">"
+ "<img src=\"" + rank.Avatar + "\" alt=\"avatar\"/>"
+ "" + rank.LastName + " " + rank.FirstName + ""
+ "</div>"
+ "<div class=\"rank\">"
+ "rank: " + rank.Rank + ""
+ "Kết bạn"
+ "</div></li>";
}
ltrListSug.Text = str;
}
}
}`
It's a user control SugFriends.ascx being add in Default.Master
When I click "a.button" postback event to server?(I want to insert data to database, I used Sql server)
How to do it? Make tag a event onclick 'like' LinkButton: Onclick()?
Thanks for your helping! I found solution that problem! I used Ajax onclick for each tag a, event post Ajax.aspx, in here I can code work with database!
Add an onclick="(javascript:__doPostBack('','');" attribute to the <a> tag to perform a postback via Javascript. Like this:
+ "Kết bạn"
More details here
Response to comment:
Try creating a button on the page
<asp:Button ID="btnPlaceHolder" Visible="False" runat="server" /> and then do what I said in this solution but instead use __doPostBack('<%=btnPlaceHolder.UniqueID %>', '')
From there, you can use the method block
Private Sub btnPlaceHolder_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPlaceHolder.Click to run the code you want on postback.

Categories

Resources