Requesting a page from GridView's RowCommand event, here is the code
protected void grdClaimList_RowCommand(object sender, GridViewCommandEventArgs e)
{
switch (e.CommandName)
{
case "ViewClaim":
Response.Redirect("ClaimStatus.aspx?id=" + e.CommandArgument);
break;
}
}
I would like hide the query string from url, is it possible? if yes, please let me know how?
You can also use session variables or view states instead.
I don't think it is possible but you could encrypt in and then decrypt it on the other side if you don't want the User seeing it and putting different values in.
Related
I have a page that shows some data depending on the id param in request.
The user must be logged in
How should I stop the user from accessing certain ids that belong to other users. Is redirect the best choice?
You can check on the server-side before rendering the page, then redirect...etc:
void Page_Load(object sender, EventArgs e)
{
string bla = Request["key"] != null ? Request["key"] : "";
if(String.IsNullOrEmpty(bla))
if (bla=="yourvaluetocheck" && !User.IsInRole("theroletouse"))
{
Response.Redirect("~/permissiondenied.aspx");
}
}
This a very simple example, I would put this type of logic in a central place in my application for re-use.
I am creating page for admin to view all user in the system database. I’m using gird view to retrieve all the users in membership table. The problem now is how can Admin edit, delete and update the changes made by the admin? When we want to configure the select statement, there's advance button which we can put some additional statements. The membership table in my SQL doesn’t have a primary key. How do I solve this? Much thanks.
Have a look at this tutorial, it does exactly what you ask http://www.codeproject.com/Articles/24085/Insert-Update-Delete-with-Gridview-simple-way
That tutorial that Ashwin suggested looks way too involved for me. The direction I would take...
Store the username field in the datakey property of the gridview. And use the RowDeleting and RowUpdating events of the grid view...
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
e.Cancel = true; // cancel default action.
// delete user myself.
string user = e.Keys["username"].ToString(); // think that's the name of the field in database.
Membership.DeleteUser(user);
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
e.Cancel = true; // cancel default action.
// update user myself.
var userToUpdate = new MembershipUser();
// get new values with e.NewValues[] and fill out all properties of userToUpdate.
Membership.UpdateUser(userToUpdate);
}
Calling methods from the membership object seems much easier in my opinion, and then you don't have to mess with the tables that asp.net generates which could mess something up if done incorrectly.
In my code I have a dropdown selection and upon selection from dropdown the code performs further processing and generates report/data.
Further, the entire program depends on data which is gathered from 3 different operation
Operation1: processing a text files of size of size > 6MB
Operation2: SQL Query to a DB (Query takes around 1 minute)
Operation 3: HTTP POST request to server (The main costliest part of the programe)
So, to make it efficient I am thinking to perform this operation only once and use the data for all the different selection from dropdown.
Question is how can I do so as below:
I can't put it in "page_load" event because every time page loads the operations will carry out
I can't put it inside "dropdownlist_selectedindexchanged" event because then it will be same as #1.
I thought of doing it in "page_load" as below
void Page_Load(object sender, EventArgs e)
{
if(!ispostback)
{
Operation1();
Operation2();
Operation3();
}
}
This is fine; the operations gets performed only once and I can use the data throughout, but then my page will take time to load as the operations takes time.
Is there any other way I can achieve what I want? Please let me know.
Thanks,
Rahul
If the data set will not change, you probably could manage to do it once at Application_Start().
Edit - something like this (typing from memory and away from VS, i do VB):
Protected void page_load(object sender, eventargs e)
{
// the name can be anything
if (!System.Web.HttpContext.Current.Session["data_cache_filled"])
{
// code to fill the cache.
// ...
//mark it as filled
System.Web.HttpContext.Current.Session["data_cache_filled"] = "yes";
}
}
Cache it. Using the CacheHelper class from here, you could do:
internal List<Employee> Operation1()
{
List<Employee> employeeData;
if (!CacheHelper.Get("employeeData", out employeeData))
{
employeeData = (from x in db.Employees select x).ToList(); // or whatever
CacheHelper.Add(employeeData, "employeeData");
}
return employeeData;
}
I have implement GridView Row Editing feature in my .net application using <asp:CommandField.
I clicked on Update button to save the record after editing the row.Now if i refresh the page or press F5 GridView_RowCommand fired again.
How can we avoid this.Is there any mechanism to identify when user press F5 OR refresh the page.Is there any method in client side or in server side.
Not exactly the best "technical" solution to your problem but you could always just do a Response.Redirect(Request.RawUrl) once you have finished doing anything you need to do in your RowCommand
Like I said, it's not the best "technical" solution but it is a solution
Dave
One method of capturing this is to maintain a session variable that is related to the page in question. In the session variable you would keep some kind of state enumeration, key or string that would determine the last action taken. You could even use a simple incremented counter, and if you ever received a postedback counter that was equal to the session variable it would indicate a page refresh rather than a new action.
Session["LastInsertedItem"] = null;
MyCustomObjType myCustomObject;
protected void Page_Load(object sender, EventArgs e)
{
myCustomObject = Session["LastInsertedItem"] as MyCustomObjType;
}
void GridView_RowCommand(Object sender, GridViewCommandEventArgs e)
{
If(myCustomObject == null || //!(compare your value with myCustomObject.field) )
{
// do your operations and save the values to myCustomObject and save that object back to Session.
}
else
{
// It is refreshed or same data is being insterted - don't know if second option is possible in your case.
}
}
I want to pass 3 parameters for SelectMethod and 1 parameters for SelectCountMethod of ObjectDataSource.
How can I pass these? And how ObjectDataSource can distinguish which parameters for which methods?
There are two ways of passing parameters to an ObjectDatasource.
1) Through it's wizard you can bind the parameters to various controls, form fields, querystring, session, etc.
2) In it's Selecting event. Example:
protected void Page_Load(object sender, EventArgs e)
{
myObjDs.Selecting += new ObjectDataSourceSelectingEventHandler(myObjDs_Selecting);
}
void myObjDs_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
{
e.InputParameters["someparamname"] = "test";
}
Instead of using selecting event you can also directly add parameters in your button click or anyother function . It must differentiate on the basis of parameter name . I haven't tested it but it shall work.
ObjectDataSource2.SelectParameters.Clear()
ObjectDataSource2.SelectParameters.Add("Parameter1",ValueOfParameter1);