Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. HOW CAN I ACCESS THE BUTTON CONTROL FROM ASPX IN ASPX.CS?

HOW CAN I ACCESS THE BUTTON CONTROL FROM ASPX IN ASPX.CS?

Scheduled Pinned Locked Moved ASP.NET
questiondatabasehelptutorial
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Albert83
    wrote on last edited by
    #1

    Hi Everyone, I get the error:The name 'postComment' does not exist in the current context groups.aspx.cs doesn't see what's in the groups.aspx. I can't use the option of putting the code inside the aspx together. I would like to know how to access the textbox from the aspx.cs page I have two pages: groups.aspx groups.aspx.cs I have created a button in the groups.aspx as follows: Now I want to access it from the groups.aspx.cs to insert the contents of what was entered in the text box to the database as follows: protected void postBtn_Click(object sender, EventArgs e) { SqlDataSource PostDS = new SqlDataSource(); PostDS.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString(); PostDS.InsertCommandType = SqlDataSourceCommandType.Text; PostDS.InsertCommand = "INSERT INTO GroupChalkboard (Text, GroupID, UserID, CreatedDate) VALUES (@Text, @GroupID, @UserID, @CreatedDate)"; PostDS.InsertParameters.Add("Text", postComment.Text); PostDS.InsertParameters.Add("GroupID", pid.ToString()); PostDS.InsertParameters.Add("UserID", User.Identity.Name); PostDS.InsertParameters.Add("CreatedDate", DateTime.Now.ToString()); PostDS.Insert(); } Thank you.

    M 1 Reply Last reply
    0
    • A Albert83

      Hi Everyone, I get the error:The name 'postComment' does not exist in the current context groups.aspx.cs doesn't see what's in the groups.aspx. I can't use the option of putting the code inside the aspx together. I would like to know how to access the textbox from the aspx.cs page I have two pages: groups.aspx groups.aspx.cs I have created a button in the groups.aspx as follows: Now I want to access it from the groups.aspx.cs to insert the contents of what was entered in the text box to the database as follows: protected void postBtn_Click(object sender, EventArgs e) { SqlDataSource PostDS = new SqlDataSource(); PostDS.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString(); PostDS.InsertCommandType = SqlDataSourceCommandType.Text; PostDS.InsertCommand = "INSERT INTO GroupChalkboard (Text, GroupID, UserID, CreatedDate) VALUES (@Text, @GroupID, @UserID, @CreatedDate)"; PostDS.InsertParameters.Add("Text", postComment.Text); PostDS.InsertParameters.Add("GroupID", pid.ToString()); PostDS.InsertParameters.Add("UserID", User.Identity.Name); PostDS.InsertParameters.Add("CreatedDate", DateTime.Now.ToString()); PostDS.Insert(); } Thank you.

      M Offline
      M Offline
      Michael Sync
      wrote on last edited by
      #2

      Albert83 wrote:

      I have two pages: groups.aspx groups.aspx.cs

      Actually, those are not two pages. This is just one page and its code-behind file. 1. In Groups.aspx, the code file should be set to groups.aspx.cs. CodeFile="groups.aspx.cs" 2. Check whether that textbox has runat="Server" or not.

      Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net)

      A 1 Reply Last reply
      0
      • M Michael Sync

        Albert83 wrote:

        I have two pages: groups.aspx groups.aspx.cs

        Actually, those are not two pages. This is just one page and its code-behind file. 1. In Groups.aspx, the code file should be set to groups.aspx.cs. CodeFile="groups.aspx.cs" 2. Check whether that textbox has runat="Server" or not.

        Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net)

        A Offline
        A Offline
        Albert83
        wrote on last edited by
        #3

        The thing is that I have created the button and the textbox inside the Datalist, and because of that I can't access it directly. I don't know how to access it from the DataList. Here is the code: I removed the open tags because it's not showing the code otherwise: asp:datalist id="DataList1" runat="server" datasourceid="ChalkDL"> itemtemplate> asp:textbox id="postComment" runat="server"> asp:button runat="server" onclick="postBtn_Click" id="postBtn" text="Post!" /> /itemtemplate> /asp:datalist>

        M 1 Reply Last reply
        0
        • A Albert83

          The thing is that I have created the button and the textbox inside the Datalist, and because of that I can't access it directly. I don't know how to access it from the DataList. Here is the code: I removed the open tags because it's not showing the code otherwise: asp:datalist id="DataList1" runat="server" datasourceid="ChalkDL"> itemtemplate> asp:textbox id="postComment" runat="server"> asp:button runat="server" onclick="postBtn_Click" id="postBtn" text="Post!" /> /itemtemplate> /asp:datalist>

          M Offline
          M Offline
          Michael Sync
          wrote on last edited by
          #4

          Albert83 wrote:

          The thing is that I have created the button and the textbox inside the Datalist, and because of that I can't access it directly. I don't know how to access it from the DataList.

          There are some events called Item_DataBound or Row_Created event. In those events, you can find the control.. For example: TextBox txt = e.FindControl("controlname");

          Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net)

          A 2 Replies Last reply
          0
          • M Michael Sync

            Albert83 wrote:

            The thing is that I have created the button and the textbox inside the Datalist, and because of that I can't access it directly. I don't know how to access it from the DataList.

            There are some events called Item_DataBound or Row_Created event. In those events, you can find the control.. For example: TextBox txt = e.FindControl("controlname");

            Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net)

            A Offline
            A Offline
            Albert83
            wrote on last edited by
            #5

            I just find a possible solution but for some reason I get an error: System.Web.UI.WebControls.TextBox PostText = (System.Web.UI.WebControls.TextBox)DataList1.FindControl("postComment"); The name 'DataList1' does not exist in the current context The data list was declared as: asp:datalist id="DataList1" runat="server" datasourceid="ChalkDL"> And what's weird is that I can find DataList1 as an option in the properites and methods drop list when I start typing it. What can be the reason for it?

            1 Reply Last reply
            0
            • M Michael Sync

              Albert83 wrote:

              The thing is that I have created the button and the textbox inside the Datalist, and because of that I can't access it directly. I don't know how to access it from the DataList.

              There are some events called Item_DataBound or Row_Created event. In those events, you can find the control.. For example: TextBox txt = e.FindControl("controlname");

              Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net)

              A Offline
              A Offline
              Albert83
              wrote on last edited by
              #6

              Here is the code: protected void postBtn_Click(object sender, EventArgs e) { SqlDataSource PostDS = new SqlDataSource(); System.Web.UI.WebControls.TextBox PostText = (System.Web.UI.WebControls.TextBox)DataList1.FindControl("postComment"); PostDS.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString(); PostDS.InsertCommandType = SqlDataSourceCommandType.Text; PostDS.InsertCommand = "INSERT INTO GroupChalkboard (Text, GroupID, UserID, CreatedDate) VALUES (@Text, @GroupID, @UserID, @CreatedDate)"; PostDS.InsertParameters.Add("Text", PostText.Text); PostDS.InsertParameters.Add("GroupID", pid.ToString()); PostDS.InsertParameters.Add("UserID", Session["UserID"].ToString()); PostDS.InsertParameters.Add("CreatedDate", DateTime.Now.ToString()); PostDS.Insert(); }

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups