Jan
16
2007
JSDropdownlist part 2 - what about the javascript
Posted by admin under
Controls
This is part 2 in this article serie. Please start from the beginning
Now - we are not totally finished when it comes to the original app. As I said it has some special requirements - for example the Delete button should only be available to superadmins - plus we need to throw in an warning message when clicked:
Hiding/showing the buttons according to rights:
In the download I have faked the login procedure:


The code for this is simple - I set autopostback to true for the checkbox, set a session variable Session["admin"] and then in our databind procedure we check that value:
protected void grdProducts_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRowView oRow = e.Row.DataItem as DataRowView;
Label lblId = e.Row.FindControl("lblId") as Label;
lblId.Text = oRow["productid"].ToString();
Label lblName = e.Row.FindControl("lblName") as Label;
lblName.Text = oRow["productname"].ToString();
Button oBut = e.Row.FindControl("btnEdit") as Button;
oBut.CommandArgument = oRow["productid"].ToString();
oBut = e.Row.FindControl("btnDelete") as Button;
oBut.CommandArgument = oRow["productid"].ToString();
if (IsCurrentUserSuperAdmin()== false)
{
oBut.Visible = false;
oBut.Enabled = false;
}
}
}
private bool IsCurrentUserSuperAdmin()
{
//faked...
return Session["admin"] == null ||
Session["admin"].ToString() == "" ? false : true;
}
Javascript warning for our delete button
Now for the Warning. It's pretty simple since we already have the reference to each Delete button in the databind procedure:
oBut = e.Row.FindControl("btnDelete") as Button;
oBut.Attributes.Add("onclick",
"return confirm('Are you sure you want to delete this?');");

So far so good - now, however, consider this application a few months later. It has been a success to the users - and therefore now one should be able to do more things with each product:
- reorder (superadmin only)
- see/manage pictures (all)
- see sale sats
- create sale campaign
etc.
Now (at least this is what happened to my system) it would look like this:

CONTINUE PART 3 - Download