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)
No comments:
Post a Comment