Tuesday, August 25, 2009

Adding command to Item in DataList

This is much simple and I did it without tutorial from the context of command knowledge.Lets make an overview here. Here is the HTML code that adds a command to the link button which is a control of a DataList Item:

1. HTML code
< asp:DataList >
< ItemTemplate >
< ItemTemplate >
< asp:LinkButton ID="hypTest" runat="server" CommandName="MySelect" >< strong >< %# DataBinder.Eval(Container.DataItem, "BusinessName") % >< /strong >< /asp:LinkButton >
< br / >
< asp:Label runat="server" Text='< %# DataBinder.Eval(Container.DataItem, "ShortDescription") % >' BorderStyle="None" Height="48px" Width="180px" >< /asp:Label >
< asp:Label ID="conidLabel" runat="server" Text='< %# "contractordetails.aspx?conid=" + DataBinder.Eval(Container.DataItem, "contractorid") % >' Visible="false" >< /asp:Label >
< asp:Label ID="contid" runat="server" Text='< %#Eval("contractorid") % >' Visible="false" >< /asp:Label >
< br / >
< /ItemTemplate >
< /ItemTemplate >
< /asp:DataList >

Here we can see that how a command is added to the link button for issuing command which we will use later.

2. Programmer's Task:
Here is the code what programmer will do. Actually here I have done a code for updating balance for a contractor for each client click.

try {
DataListItem myselectedItem = e.Item;
Label myLabel = myselectedItem.FindControl("conidLabel") as Label;
Label contid = myselectedItem.FindControl("contid") as Label;


if (e.CommandName == "MySelect")
{

/*click fee handling code:here*/
try
{

int catid = int.Parse(Session["catid"].ToString());
float clickfee = 0;

/*getting click fee*/
try {
Category category = new Category();
if (category.LoadByPrimaryKey(catid))
{
clickfee =float.Parse( category.ClickFee.ToString());
}
/*click fee obtained*/

}
catch (Exception exc) {
StatusLabel.ForeColor = Color.Red;
StatusLabel.Text = "Failed to retrieve click fee!";
}

/*subtracting from balance*/
try {
Contractor contractor = new Contractor();
int contractorid = int.Parse(contid.Text);
if (contractor.LoadByPrimaryKey(contractorid))
{
float currentBalance =float.Parse( contractor.PrepaidAmount.ToString());
currentBalance = currentBalance - clickfee;
contractor.PrepaidAmount = currentBalance;
/*saving change*/
contractor.Save();
}

}
catch (Exception exc)
{
StatusLabel.ForeColor = Color.Red;
StatusLabel.Text = "Failed to subtract click fee from balance!";
}


}
catch (Exception exc) { }

/*Redirecting to the contractor details page*/
string redirect_url = myLabel.Text;
Response.Redirect(redirect_url);

}

//StatusLabel.Text = "done!";
}
catch (Exception exc)
{
StatusLabel.ForeColor = Color.Red;
StatusLabel.Text = exc.Message;
}


Here we can see we can get the selected item using
DataListItem myselectedItem=e.Item;
whereas for getting the selected row from GridView we had to do a lot of work.
However, Now we can access this item for further operation like getting some hidden value and use them for further operatiuons:

Label myLabel = myselectedItem.FindControl("conidLabel") as Label;
Label contid = myselectedItem.FindControl("contid") as Label;

I think this will help.

Thanks
Masud (25-08-2009)

Saturday, August 15, 2009

Selecting a item from Dropdownlist

This is an interesting thing I did few days ago while I was working in my office. Suppose the DDL(Dropdownlist) is containing some items. You wanna select one from the list programmatically at runtime. Generally at first I tried to do in this way:

MyDDL.Text="item text";
But this will not work and actually it will not also show any error. You will just try and try but nothing will happen. So we need to click the right way.

As we already saw the two things for databound DDL is:
* DataTextField
* DataValueField

Suppose Subcategorydesc=dataTextField
SubCatID=datavaluefield

And we want to select a subcategory called "Marketing" having subcatid=3. Then the code will be like this:

MyDDL.Items.getByValue("3").Selected=true;

thanks
Masud.

Wednesday, August 5, 2009

Gridview editing problem: Textbox cant store new value in the editing mode

This is a common problem I have faced during my work. Every events are properly handled you will not get the new value in the update time. This is ridiculous thing and often your head can be cracked. But the solution to this problem is quite simple. Lets look at the example that I did....

Suppose this is the code for Update:

protected void ImgGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
/*Image info to be updated*/
try {
/*Extracting different elements from Gridview*/
GridViewRow myRow = ImgGridView.Rows[e.RowIndex];
/*getting pic id*/
Label PicIDLabel = myRow.Cells[2].FindControl("PicIDLabel") as Label;
/*getting image title*/
TextBox titleTextBox = myRow.Cells[1].FindControl("ImgTitleTextBox") as TextBox;


/*updating ad image*/
AdImage adImage = new AdImage();
adImage.ConnectionString = ConfigurationManager.ConnectionStrings["BIZNESConnectionString"].ConnectionString;
if(adImage.LoadByPrimaryKey(short.Parse(PicIDLabel.Text)))
{
adImage.ImgTitle=titleTextBox.Text;

adImage.Save();

}

ImgGridView.EditIndex = -1;
showAdImageInfo();

StatusLabel.ForeColor=Color.Green;
StatusLabel.Text="Image information updated successfully!";
StatusImage.ImageUrl = "adimages/ok.gif";


}
catch (Exception exc)
{
StatusLabel.Text = exc.Message;

}

}

So found no problem in update code every time I debug the code ....I spend the whole day on it and at last I found a forum that probably I have to check for autopostback option of the page.
So my Page_Load was like this:
protected void Page_Load(object sender, EventArgs e)
{



string access = (string)Session["access"];
/*checking access*/
if (access != "yes")
Response.Redirect("SellerAdminLogin.aspx"

/*showing images*/
showAdImageInfo();


}
For that killing problem to resolve we have to check the postback option. Lets look at the editing function:

protected void ImgGridView_RowEditing(object sender, GridViewEditEventArgs e)
{

/*Image editing code*/
ImgGridView.EditIndex = e.NewEditIndex;
showAdImageInfo();

}

If autopost-back is not checked every time gridview will loaded with values from databse so when update command will be clicked then data will be loaded from database to the textbox and new value in the textbox will be lost. So we need to check the auto-postback option:

protected void Page_Load(object sender, EventArgs e)
{


if(!Page.IsAutoPostBack)
{
string access = (string)Session["access"];
/*checking access*/
if (access != "yes")
Response.Redirect("SellerAdminLogin.aspx"

/*showing images*/
showAdImageInfo();

}
}

Thus update will easily work and we will be able to update the gridview data

thanks.
Masud

Tuesday, August 4, 2009

Image Carousel Tools

I came to learn a very interesting thing which is implemented using JQuery and named as Image carousel. I came to learn from my mentor while doing the project
http://nochallenge.net/biznesmakler.
Here I have made some cosmetic customization which evolves the existing carousel to a professional level as said by project manager. However I will be dealing with next couple of my posting with image carousel as well as lightBox slideshow

thanks.
Masud

Adding Select text in databound Dropdownlist

To create a data-bound dropdownlist we have to follow several steps. This time we are using Business entity which is CSharp Business Entity template and it is same as creating the business object. Suppose a business entity Category having three fields.

Category:
CatID
CatDescription
Active

Now we want to make a databound dropdownlist(DDL). Lets call the dropdownlist is CatDropDownList. So we have 2 steps:
1. Creating databound DDL
2. Inserting select text

Here we go:
1. Creating databound DDL.
try
{
Category category=new Category();

/*Generating and executing query*/
category.Query.AddResultColumn(Category.ColumnNames.CatID);
category.Query.AddResultColumn(Category.ColumnNames.CatDescription);
category.Query.Load();

/*databinding*/
this.CatDropDownList.Datasource=category.DefaultView;
this.CatDropDownList.DataTextField=Category.ColumnNames.CatDescription;
this.CatDropDownList.DataValueField=Category.ColumnNames.CatID;
this.CatDropDownList.DataBind();

2.Inserting Select text:
Generally if you add item now then DDL will not retain that item. this time you have to insert the values in the DDL. The required code is:
this.CatDropDownList.Items.Insert(0,"select a category");

}
catch(Exception exc){}

Now comes how use the value selected by the DDL.This is quite simple.
int selectedCatID=Convert.ToInt32(CatDropDownList.SelectedValue);
here selectedCatID will give the selected CatID of the category you selected....

Thanks.
Masud (5.8.9)