Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 343
  • Last Modified:

CommandArgument and Dictionary...

Hi,
I built an Click Event for Button and i need to pass to another function A Dictionary.
The Event :
protected Dictionary<string, object> LogGetField;
protected void submitButton_Click(object sender, CommandEventArgs e)
    {
          LogGetField = GetValues();        
          UpdateDataGridCondition(LogGetField);
          Button bt = new Button();
          bt.CommandName = "ButtonClicked";
          bt.CommandArgument = LogGetField;     ------------------>>> HERE IS THE PROBLEM
    }

OK, the problem is that i need to pass a Dictionary, and   bt.CommandArgument   gets a string.
Is it possible to let the bt.CommandArgument  have a Dictionary ?
Thanks
0
udir
Asked:
udir
  • 2
1 Solution
 
discon0Commented:
Unfortunately no. The CommandArgument property can only be a string.

You have some other options:
1) Convert your dictionary to a string (serialize it), assign it to the CommandArgument and then convert it back to a dictionary when you need it. This would not be too hard if your Dictionary was <string, string> or some other type that can be easily converted to string but your values are objects.

2) You can add the dictionary to the Session using a specific key and set the CommandArgument to that key. You can later retrieve the dictionary from the Session using the CommandArgument.

Example:
protected void submitButton_Click(object sender, CommandEventArgs e)
    {
          LogGetField = GetValues();        
          UpdateDataGridCondition(LogGetField);
          Button bt = new Button();
          bt.CommandName = "ButtonClicked";

          string key = "lgf" + LogGetField.GetHashCode().ToString();
          Session[key] = LogGetField();
          bt.CommandArgument = key;
    }

to retrieve the dictionary:

          Dictionary<string, object> d = Session[bt.CommandArgument] as Dictionary<string, object>;
          if (d != null)
          {
                    // Your code here
          }


0
 
udirAuthor Commented:
Hi,
Yha thats can be a good Idea   :)
Thanks
0
 
discon0Commented:
Hi, just an addition...

I was thinking that using LogGetField.GetHashCode() for the key may not be the best way because it doesn't guarantee uniqueness. It may be better to append a random number at the end or a Guid like Guid.NewGuid().ToString();
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now