Search code examples
c#asp.netcode-behindlinkbuttondynamic-controls

Dynamic Control used to create Link Button and assign properties on ASP.NET


I would like to generate the Link Button as below in code behind.

<asp:LinkButton ID="lnkCustomize" OnClientClick="showDialog('editPerson')" Text="Customize"  CommandName="Customize"  CommandArgument='<%#Eval("type") + ";" + Eval("facility") + ";" + Eval("timestamp")%>runat="server"></asp:LinkButton>

This is what I have so far but failed to retrieve value from arguements. Appreciate for any reply.

LinkButton link = new LinkButton();
link.Text = "Customize";
link.ID = "lnkCustomize";
double timestamp = ConvertToUnixTimestamp(leftstartTime1);
link.CommandArgument = Eval("type") + "," + Eval("facility") + "," + Eval("timestamp");
link.Command += new CommandEventHandler(DynamicClick);
link.OnClientClick = "showDialog('editPerson')";

Solution

  • 1. Fixed by having proper defined format

    link.CommandArgument = "first_parameter_value_goes_here" + "," + "second_parameter_value_goes_here" + "," + "third_parameter_value_goes_here";
    

    2. Fixed by extracting all the values of arguements as below

    public void DynamicClick(object sender, EventArgs e)
        {
            var editLink = ((LinkButton)sender);
            string info = editLink.CommandArgument;
            string[] arg = new string[2];
            char[] splitter = { ',' };
            arg = info.Split(splitter);
            var var1 = arg[0];
            var var2 = arg[1];
            var var3 = arg[2];
        }