0

I am getting column type from database and changes the textbox attribute accordingly, for example, if i have a date column to input value , i would add attribute with

 Text4.Attributes["Type"] = dataTypeList[3].ToString();

which will change the textbox to (ex. input type="date") however, when i want to get the date value , "Conversion failed when converting date and/or time from character string with Dynamic Textbox" Occurs , because i am getting it with

Text4.Value.ToString()

any clue on how i should do it? To get textbox from date dynamically not by using the .selectedDate attribute? As i would not know what input type the textbox will be. Any help is much appreciated , Thanks

2
  • Dynamic textbox means you are creating control at run time and then doing these things or you have a textbox already? Commented Mar 18, 2018 at 1:37
  • I have a textbox already but the type will change accordingly , i want to get the data from textbox too and post it to sql Commented Mar 18, 2018 at 2:58

1 Answer 1

1

Based on your explanation I have assumed certain things and tried to visualize your scenario and came across following solution

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="DynamicApp.Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="mainForm" runat="server">
        <div id="textBoxHolder" runat="server"></div>
        <asp:Button ID="btnShow" Text="Show" runat="server" OnClick="btnShow_Click" /><br />
        <asp:Literal ID="litShow" Text="" runat="server" />
    </form>
</body>
</html>

Default.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

namespace DynamicApp
{
    public partial class Default : System.Web.UI.Page
    {
        // data source
        List<string> dataTypeList = new List<string> { "text", "date", "number" };

        protected void Page_Load(object sender, EventArgs e)
        {     
            // creating dynamic textboxes

            var textBoxHolder = (HtmlGenericControl)mainForm.FindControl("textBoxHolder");
            var index = 1;

            foreach (var item in dataTypeList)
            {
                var textBox = new TextBox();
                textBox.ID = "txtBox" + (index++);
                textBox.Attributes["type"] = item;

                textBoxHolder.Controls.Add(textBox);
            }
        }

        protected void btnShow_Click(object sender, EventArgs e)
        {
            // getting textbox values

            var textBoxHolder = (HtmlGenericControl)mainForm.FindControl("textBoxHolder");

            for (int index = 1; index <= dataTypeList.Count; index++)
            {
                var textBox = (TextBox)textBoxHolder.FindControl("txtBox" + (index));
                litShow.Text += textBox.Text + "<br>";
            }
        }
    }
}

The aspx file is straight forward I have took one button and a literal; also a textBoxHolder which will hold my dynamic textboxes

In the code behind aspx.cs I have made a dummy data source "dataTypeList" with 3 different types then on the page load I am creating and adding dynamic textboxes to the page. Next on btnShow click I am just fetching values from these dynamic textboxes and displaying the text in literal

Sorry if my assumptions are not as per your scenario.

Hope it helps

Sign up to request clarification or add additional context in comments.

1 Comment

That is how i do it but for the date type if it is on the text type when i get value and want to post it to sql, error will pop out , thanks for helping

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.