Dynamic Creation of Hashtable inside Custom TemplateField Class (Complex)

Discussion in 'Microsoft ASP .NET' started by pbd22, Oct 28, 2007.

  1. pbd22

    pbd22 Guest

    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.
     
    pbd22, Oct 28, 2007
    #1
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andy Eshtry
    Replies:
    0
    Views:
    423
    Andy Eshtry
    Mar 1, 2004
  2. sloan
    Replies:
    0
    Views:
    517
    sloan
    Jun 26, 2006
  3. Stan SR

    Dynamic TemplateField

    Stan SR, Jun 16, 2007, in forum: Microsoft ASP .NET
    Replies:
    1
    Views:
    230
    Stan SR
    Jun 16, 2007
  4. mesut
    Replies:
    0
    Views:
    235
    mesut
    Jun 29, 2007
  5. pbd22
    Replies:
    0
    Views:
    181
    pbd22
    Nov 11, 2007
Loading...

Share This Page