Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Learning to use JavaScript with ASP.Net
Message
De
20/05/2009 14:26:10
Timothy Bryan
Sharpline Consultants
Conroe, Texas, États-Unis
 
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Versions des environnements
Environment:
ASP.NET
Application:
Web
Divers
Thread ID:
01400821
Message ID:
01401048
Vues:
54
Hi Paul,

This goes back to a topic I had discussed with you earlier about a grid and timing the events right to use the business object for inserts before the grid adds a row and presents it to the user for editing without all the default values. What I ended up doing is adding my own button that causes a postback (inside an update panel) to add a row to the underlying dataset and then refresh the grid. What I am trying to do with this code is to place the grid into edit mode on the last row (the one added) which makes the row editing template come up. The row is being added ok and is displayed in the grid, but the template is not opening because I can't find a way to run my javascript to place that row into edit mode. Here is what I have.

In my ASP page I have this hidden field.
<asp:HiddenField ID="hdnIsNew" runat="server" Value="N" />
In my button code I am setting it to "Y" as below.
protected void btnAddTruck_Click(object sender, EventArgs e)
{
	// Retrieve the previous Trucks DataSet
	this.LoadUpdateableTrucksDataSet();

	// Add a new Row to the DataSet
	this.oTruck.NewRow(this.dsTrucks);

	// Set the hidden field on the Client Side
	hdnIsNew.Value = "Y";

	// Store the DataSet back into the Session
	this.SaveUpdateableTrucksDataSet();

	// Set the row for Editing  - Apparently we can't do this here, 
	// we need to do this on client side after it returns.
	//this.grdTrucks.EditIndex = dsTrucks.Tables[this.oTruck.TableName].Rows.Count - 1;

	// Rebind the Grid
	this.BindControl(this.grdTrucks);
}
In my script file I have these functions that I thought would run.
function grdTrucks_InitializeGrid(gridId) {
    var oGrid = igtbl_getGridById(gridId);
    // Does this run after a postback also
    // Can i check here for the hidden field and
    // call EditAfterNew() if "N"?
    var hidden = document.getElementById("hdnIsNew");
    if (hidden.value == "Y") {
        EditAfterNew(gridId);

        // Set the hidden value back
        hidden.value = "N";
    }
   }

// This is to place the newly added row in edit mode
function EditAfterNew(grdId) {
    // Cause the new row to be placed in Edit mode with Template
    var oGrid = igtbl_getGridById(gridId);
    var lastRow = oGrid.rows.getRow(oGrid.Rows.length - 1);
    lastRow.Activate();

    // Place the row into template edit
    oGrid.BeginEditTemplate();
  }
This doesn't work and I am a bit confused about the code you posted that would cause a script to run after the browser is loaded. Where do I put code to inject script inito the page?

Thanks for your help
Tim


>
><script language="javascript" type="text/javascript"
>   function SetValue()
>   {
>      var myControl = document.getElementById('txtHidden');
>      if (myControl)
>         document.forms[0].submit();
>        // Instead of this you can use ASP.NET's __doPostback code to fire the postback. If you use it, it's then possible to intercept this info in the page
>        // __doPostBack('ControlOrEventCausingPostback', 'AnyParameters');
>        // In that case, you don't need the hidden input - pass the parameter as the second parameter of __doPostBack
>   }
></script
><input type="hidden" name="txtHidden" /
>
>// In your ASP.NET code-behind:
>if (Page.IsPostBack)
>{
>    string hiddenValue = Request.Form["txtHidden"];
>    // Do something
>    // Or if you are using ASP.NET's __doPostBack you can do this in Page_Load:
>    // string eventName = Request.Form["__EVENTTARGET"];
>    // string arg = Request.Form["__EVENTARGUMENT"];
>    // if (eventName == "ControlOrEventCausingPostback")
>    //    this.DoSomething(arg);
>}
>
>
>
>You can't easily persist variables between postback w/o just recreating them. What I normally do is just inject some JavaScript after the postback which calls the function you want, ex:
>
>
>string script = "<script type=\"text/javascript\">JSFunctionToRun();</script>";
>Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "JustNameOfScript", script);
>// You can actually call RegisterClientScriptBlock with a 4th parameter of true - that will add the <script tag stuff for you.
>
>
>This just tells ASP.NET to inject this JS code into the page. As soon as the browser loads it should run this code.
Timothy Bryan
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform