Dynamic Creation of Hashtable inside Custom TemplateField Class (Complex)

P

pbd22

Hi.

I have a somewhat complex problem (at least for me).

I need on help on Databinding a hashtable in a custom TemplateField
class.

I would really appreciate it if you stick with me here, as I think I
am close but
need some fresh suggestions.

Ok. I have a GridView.
The GridView has three visible rows.

In the same enclosing table of each given visible column,
I want to add related hidden fields.

The data for the hidden fields are stored using a
Hashtable.


for (int i = 0; products != null && i < products.Length; i++)
{

if (products != null)
{

//Build the visible column rows
DataRow drow = dt.NewRow();

// THESE ARE THE COLUMNS

drow[NAME] = GetSKU(products.data,
NAME);
drow[REGION] =
GetTitle(GetSKU(products.data, REGION));
drow[TYPE] = "Local";

// HIDDEN INPUTS AS SIBLINGS OF THE
ABOVE DATA

htable = new Hashtable();

htable.Add(DESCRIPTION,
GetSKU(products.data, DESCRIPTION));
htable.Add(Size,
GetSKU(products.data, SIZE));
htable.Add(Price,
GetSKU(products.data, PRICE));

dt.Rows.Add(drow);

}

}

#endregion


OK. I hope you are still with me. :)
Now, In the GridView, I have created a templatefield with two
controls:

1) <asp:LinkButton>
2) <asp:HtmlInputHidden>

The below loop accesses the three columns and builds its associated
controls. I have
a custom class called GridViewHyperlinkTemplate which creates the
linkbutton and
htmlinputhidden tags inside a TemplateField:

//Iterate through the columns of the datatable
to set the data bound field dynamically.
foreach (DataColumn col in dt.Columns)
{

switch (col.ColumnName)
{
case "Name":

TemplateField tf = new
TemplateField();

tf.ItemTemplate = new
GridViewHyperlinkTemplate(DataControlRowType.DataRow,
col.ColumnName,
col.ColumnName,

htable[DESCRIPTION].ToString(),
htable[SIZE].ToString(),
htable[PRICE].ToString());

// add to the GridView
vGridView.Columns.Add(tf);

break;

default:

BoundField bfield = new
BoundField();
bfield.DataField =
col.ColumnName;


vGridView.Columns.Add(bfield);

break;

}
}


OK. Finally, below is the GridViewHyperlinkTemplate class. THis is
where I am really lost.
How do I a get my hashtable to output the name/value pairs necessary
to create my hidden input
fields alongside the linkbuttons? I feel like I got close but am
messed up with passing the hashtable data.

public class GridViewHyperlinkTemplate : ITemplate
{
private DataControlRowType templateType;
private string columnName;
public string clientID;
private string text;
private string hiddenDESC;
private string hiddenDUR;
private string hiddenGID;

public GridViewHyperlinkTemplate(DataControlRowType type,
string colname, string Text, string desc, string dur, string
gid)
{
templateType = type;
columnName = colname;
hiddenDESC = desc;
hiddenDUR = dur;
hiddenGID = gid;
text = Text;
}

public void InstantiateIn(System.Web.UI.Control container)
{
switch (templateType)
{

case DataControlRowType.DataRow:

LinkButton lb = new LinkButton();
lb.CssClass = "ReportNoWrap";
lb.DataBinding += new
EventHandler(this.lb_DataBinding);

lb.ID = (new Random().NextDouble() * 100).ToString();
lb.OnClientClick = "javascript:playSelection(this.id);
return false;";
lb.Attributes.Add("onmouseover",
"javascript:ShowVidDetails(); return false;");
lb.Attributes.Add("onmouseout",
"javascript:HideVidDetails(); return false;");

container.Controls.Add(lb);

HtmlInputHidden hih = new HtmlInputHidden();
hih.DataBinding += new
EventHandler(this.hih_DataBinding);

hih.ID = (new Random().NextDouble() * 100).ToString();

container.Controls.Add(hih);
break;

default:
break;
}
}

private void lb_DataBinding(Object sender, EventArgs e)
{
LinkButton lb = (LinkButton)sender;
GridViewRow row = (GridViewRow)lb.NamingContainer;
//hl.NavigateUrl = DataBinder.Eval(row.DataItem,
url).ToString();
lb.Text = DataBinder.Eval(row.DataItem, text).ToString();
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////// I GET PRETTY LOST HERE. HOW SHOULD I BE ATTATCHING
DATA??/////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

private void hih_DataBinding(Object sender, EventArgs e)
{

HtmlInputHidden hih = (HtmlInputHidden)sender;
GridViewRow gvr = (GridViewRow)hih.NamingContainer;

hih.Name = DataBinder.Eval(gvr.DataItem,
"Description").ToString();
hih.Value = DataBinder.Eval(gvr.DataItem,
hiddenDESC).ToString();

}

}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

The final result, which should sit in the same DOM level as the
LinkButton elements, should (roughly) look like
like this. I say roughly because I haven't figured out the naming
conventions yet:

<INPUT id=27.8669160920507 type=hidden value=\"Yummy Potatos\"
name=27.8669160920507><INPUT id=27.8669160920507 type=hidden value=0
name=27.8669160920507><INPUT id=27.8669160920507 type=hidden value=200
name=27.8669160920507>

I REALLY APPRECIATE YOUR HELP!

thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,710
Latest member
bernietqt

Latest Threads

Top