Code for page.clientscript.registerstartupscript not working - asp.net

I am using Page.ClientScript.RegisterStartupScript(); to show a message on button click in asp.net C#.
The code is shown below :
string javaScript1 = "<script language=JavaScript>\n" + "alert('Can't add new entry since course id already exists for the selected year.');\n" + "</script>";
RegisterStartupScript("image1_ClickScript", javaScript1);
This code is written inside button click event function. Same code on same page in another function is working properly, but its not working. I have checked through debugger, there is no error, but the alert box doesnot appear on browser.
Any suggestion will be appreciated in the same direction.

Try this in a single line:
Page.ClientScript.RegisterStartupScript(GetType(), "msgbox", "alert('Can't add new entry since course id already exists for the selected year.');", true);

string javaScript1 = "\n" + "alert('Cant add new entry since course id already exists for the selected year.');\n" + ""; RegisterStartupScript("image1_ClickScript", javaScript1);
try this. looks like single quote in Can't causing an issue.

Related

Image won't display when using session in .NET

I am working with a session and I want to display image when choosing it from a gridview, so what I have two gridviews the first contain the rows from the database the second should contain the rows chosen from the first one the issue that I have is the first display image with no problem but the second won't show up any.
This is my code for the first one
foreach (CONTENT c in ls)
{
string ext = Helper_GetExtensionFromMimeType(c.MimeType);
c.TmpFilename = string.Format("~/Images/Contents/Content-{0}{1}", c.ContentID, ext);
}
gridview1.DataSource = ls;
gridview1.DataBind();
The code for the second one is (the session name is panier)
CONTENT dummy;
dummy = new CONTENT();
dummy.TmpFilename = Server.MapPath(string.Format("~/Images/Contents/Content-{0}{1}",
c.ContentID, ext));
panier.Add(dummy);
gridview2.DataSource = panier;
gridview2.DataBind();
It will be hard to determine what the issue exactly is without seeing your view code.
Anyway, make sure the image indeed exists in the generated location.
Place a breakpoint after the line dummy.TmpFilename and ensure the link is indeed formatted with the correct c.ContentID and ext, then copy the link to the browser making sure the image exists.
i found a solution to the probleme and here it is if anyone is interested
i replace
dummy.TmpFilename = Server.MapPath(string.Format("~/Images/Contents/Content-{0}{1}",c.ContentID, ext));
with
dummy.TmpFilename =string.Format("~/Images/Contents/Content-{0}{1}",c.ContentID, ext);

User text box input is rewritten by default value

Consider the following .aspx page:
Title:
<asp:TextBox runat="server" ID="TitleTB"></asp:TextBox>
Details:
<asp:TextBox runat="server" ID="DetailsTB"></asp:TextBox>
<asp:Button runat="server" ID="Btn" Text="Submit" OnClick="Btn_click"/>
Note that I minimized the code to be legitimate so a lot of lines are missing (irrelevant lines, <br/> for example).
In the C# file, I usually post the details to the database (inserting them), but if I have a certain field in my query string (to_edit, per se) I need to update the, already existing, record.
Obviously, this task is overall simple. The thing is, that when that field is included, I initially (in the Page_Load event) set the Title and the Details fields to the values already found in the record (so the user won't have to input from zero, he'd have the ones he already entered to edit).
A debug would show, though, that when I post these back to the database (UPDATE query, which looks a bit like this UPDATE Table SET Title = #Title, Details = #Details WHERE ID=#ID, where I checked #ID - which is completely valid. #Title corresponds to TitleTB.Text and #Details to DetailsTB.Text, both added with SqlCommand.AddWithValue()) that DetailsTB.Text and TitleTB.Text are, for some reason, the same as I assigned them in the Page_Load although I deleted the whole text box content in my browser and refilled it with a different string.
Here are chosen parts of my Code Behind:
//A part of my Page_Load()
//reader is an SqlDataReader, the connection is valid, the values here are valid, and the output is as planned.
TitleTB.Text = (string)reader["Title"];
DetailsTB.Text = (string)reader["Details"];
And up to now, everything seems fine.
//Relevant parts of Btn_click()
cmd.Connection = conn; //valid connection
cmd.CommandText = "UPDATE QuestionsTable SET Title = #Title, Details = #Details WHERE ID=#ID";
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("#Title", TitleTB.Text);
cmd.Parameters.AddWithValue("#Details", DetailsTB.Text);
cmd.Parameters.AddWithValue("#ID", Request.QueryString["to_edit"]);
conn.Open();
int affected = cmd.ExecuteNonQuery(); //affected is now 1, as expected.
conn.Close();
//There's a redirection over here.
But in the code shown above, TitleTB.Text and DetailsTB.Text are the same, it's not that the update query doesn't work. It's that, for some reason, the text boxes values won't change even when I clearly change them on my browser.
Any idea what could happen?
EDIT:
One remark is that when I use the OnClientClick event to alert the values (and then returning true, it was only for a test) - the values are identical to what I typed (not to the default value).
One of the possible reasons for such behavior is that you forgot to check if Page_Load is caused by a post back and you overwrite text both values in both get and post.
Just try to add an obvious condition:
//A part of my Page_Load()
//reader is an SqlDataReader, the connection is valid, the values here
if ( !this.IsPostback )
{
TitleTB.Text = (string)reader["Title"];
DetailsTB.Text = (string)reader["Details"];
}
Note that control values are stored in the viewstate and thus there is no need to update them during consecutive post backs.
Wrap your Page_Load event code in If(!IsPostBack){ }. The postback property of the button is refreshing the page with default value.
The code should be as follows -
If(!IsPostBack) {
//A part of my Page_Load()
//reader is an SqlDataReader, the connection is valid, the values here are valid, and the output is as planned.
TitleTB.Text = (string)reader["Title"];
DetailsTB.Text = (string)reader["Details"];
}
A good read may be helpful - http://www.codeproject.com/Articles/811684/Understanding-The-Complete-Story-of-Postback-in-AS

Pass records to a dialog

In a customised form I would have a create Purchase Menubutton which opens a dialog to create a purchase order.
But I need to select few records like one or two lines and then create purchase order only for those records. How do I do that?
Take a look on the "Create purchase order" button on the SalesTable form.
It works differently: you select the lines to purchase after you press the button, but it might work in your case also.
Also have a look on how to use multiple selected records in a grid.
Here is a piece of code which allows you to get the record from the previous form.
You have to put this piece of code in the INIT method of the dialog. So you have to override the init of the dialog.
DmoVehicleTable vehicleTable;
DmoVehicleId vehId;
// Get the vehicle ID from the previous form
if (element.args() && element.args().record())
{
switch (element.args().record().TableId)
{
case (tableNum(DmoVehicleTable)):
vehicleTable = element.args().record();
vehId = vehicleTable.VehicleId;
break;
default:
throw error (strFmt("#SYS477", this.name()));
}
}
I hope this will help you.
If you need more help: http://sirprogrammer.blogspot.com/

drupal confirm_password field set description under both textboxes in drupal

i have an confirm password field in the drupal and i need to set description for both the text boxes and i tried this.
$form['pass[pass1]']['#description'] = 'textbox1';
$form['pass[pass2]']['#description'] = 'textbox2';
is that possible . can any one please explain how to do it .
i tried this too.
$form['pass']['pass1']['#description'] = 'textbox1';
$form['pass']['pass2']['#description'] = 'textbox2';
for both code nothing changes in the gui
The password field gets split into two in the expand_password_confirm function (called pass1 and pass2 like you've got above so I guess you probably already know that). You can probably change the descriptions in a form pre render function, so in your form put:
$form['#pre_render'] = array('MYMODULE_my_form_pre_render');
And then create the following function:
function MYMODULE_my_form_pre_render(&$form) {
$form['pass']['pass1']['#description'] = 'textbox1';
$form['pass']['pass2']['#description'] = 'textbox2';
}
Hope that helps

ASP.NET EnqityDataSource WhereParameters, creates new property

I am trying to populate GridView, using EntityDataSource(code behind), I need to able to sort GridView. However when I sort i get error:
A property with name 'aspnet_Users.UserId1' does not exist in metadata for entity type
So I beleive it is because I generate where parameter in code behind:
ActiveEnqDataSource.WhereParameters.Add(new SessionParameter("aspnet_Users.UserId", TypeCode.Object, "UserName"));
Full code is :
ActiveEnqDataSource.ConnectionString = db.Connection.ConnectionString;
ActiveEnqDataSource.DefaultContainerName = "Entities";
ActiveEnqDataSource.EntitySetName = "Enquiries";
ActiveEnqDataSource.Include = "UserCars.CarModel.CarMake, Category, aspnet_Users";
ActiveEnqDataSource.EnableUpdate = true;
ActiveEnqDataSource.EnableInsert = true;
ActiveEnqDataSource.EnableDelete = true;
ActiveEnqDataSource.AutoGenerateWhereClause = true;
ActiveEnqDataSource.WhereParameters.Add(new SessionParameter("aspnet_Users.UserId", TypeCode.Object, "UserName"));
Any suggestions? Thank you very much! The gridview itself renders perfectly, only thing I cannot sort it, any "whereParameters" I add, Add 1 to the property e.g UserId1,EnquiryStatus1, ProdauctName1. etc...
I got a similar error because I was adding the where clause each time the page was posted back. Dropping my code that generated the where clause inside an IsPostback statement fixed the problem:
if (!IsPostBack) {
// code to add where parameters
}
I got the same error when I used markup to define a Where parameter AND then I added the same parameter in code. Somewhere along the line, the 1 at the end of the parameter name was added.

Resources