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. Can't resolve for a while. Link button in a datalist

Can't resolve for a while. Link button in a datalist

Scheduled Pinned Locked Moved ASP.NET
databasequestion
26 Posts 3 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 Albert83

    I have a LinkButton in a DataList. I also have a textbox in the same dataList. The LinkButton is supposed to delete the message that a user posted. Meaning the logged in user posted a comment and he will be able to delete if he wants to his message. Only his messages all other messages will not have the Delete LinkButton. How do I iterate through the DataList and only when it finds a LinkButton, detect if the username logged in posted messages that are stored in the database. I use a Session for a username and Data Reader for database - dr[0] Thanks in advance.

    N Offline
    N Offline
    Not Active
    wrote on last edited by
    #2

    Why iterate through the list? Use the CommandArgument property of the button to pass the id of message to your event handler.


    only two letters away from being an asset

    A 1 Reply Last reply
    0
    • N Not Active

      Why iterate through the list? Use the CommandArgument property of the button to pass the id of message to your event handler.


      only two letters away from being an asset

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

      Thanks for the response. How would I pass the username of the person that posted a message through commantArgument and then compare that username with the session[username]. so that if the session[username] == to the username that posted, then delete button will be shown. the data including the username comes from a database which DataList retrieves iteratively.

      N 1 Reply Last reply
      0
      • A Albert83

        I have a LinkButton in a DataList. I also have a textbox in the same dataList. The LinkButton is supposed to delete the message that a user posted. Meaning the logged in user posted a comment and he will be able to delete if he wants to his message. Only his messages all other messages will not have the Delete LinkButton. How do I iterate through the DataList and only when it finds a LinkButton, detect if the username logged in posted messages that are stored in the database. I use a Session for a username and Data Reader for database - dr[0] Thanks in advance.

        N Offline
        N Offline
        N a v a n e e t h
        wrote on last edited by
        #4

        I think easy method is to use HyperLink control instead of LinkButton. Set the URL of HyperLink to something like deletemessage.aspx?messageId=someid. In the deletemessage.aspx, do the necessary validations and delete the message and return back to the previous page. This will help you not to iterate on DataList rows which will become slower if you have many rows in DataList.

        Navaneeth How to use google | Ask smart questions

        A N 2 Replies Last reply
        0
        • N N a v a n e e t h

          I think easy method is to use HyperLink control instead of LinkButton. Set the URL of HyperLink to something like deletemessage.aspx?messageId=someid. In the deletemessage.aspx, do the necessary validations and delete the message and return back to the previous page. This will help you not to iterate on DataList rows which will become slower if you have many rows in DataList.

          Navaneeth How to use google | Ask smart questions

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

          Thanks for responding. But there are other items in the DataList = the username of who posted, date, and message. That's why I chose the DataList to retrieve it from the database. How would I use a hyperlink outside of the Data List so that it would be right below each message that has to be deleted?

          N 1 Reply Last reply
          0
          • A Albert83

            Thanks for the response. How would I pass the username of the person that posted a message through commantArgument and then compare that username with the session[username]. so that if the session[username] == to the username that posted, then delete button will be shown. the data including the username comes from a database which DataList retrieves iteratively.

            N Offline
            N Offline
            Not Active
            wrote on last edited by
            #6

            If your application is properly architected then the user who posted the message will be linked to it through the id in your datasource. So if you pass the id of the message you lookup the name and compare it.


            only two letters away from being an asset

            A 1 Reply Last reply
            0
            • N N a v a n e e t h

              I think easy method is to use HyperLink control instead of LinkButton. Set the URL of HyperLink to something like deletemessage.aspx?messageId=someid. In the deletemessage.aspx, do the necessary validations and delete the message and return back to the previous page. This will help you not to iterate on DataList rows which will become slower if you have many rows in DataList.

              Navaneeth How to use google | Ask smart questions

              N Offline
              N Offline
              Not Active
              wrote on last edited by
              #7

              N a v a n e e t h wrote:

              deletemessage.aspx?messageId=someid

              Why?? Now you need another page, deletemessage.aspx, to handle what could be done in the same page, or passed to a controller and separated from your presentation.


              only two letters away from being an asset

              A N 2 Replies Last reply
              0
              • A Albert83

                Thanks for responding. But there are other items in the DataList = the username of who posted, date, and message. That's why I chose the DataList to retrieve it from the database. How would I use a hyperlink outside of the Data List so that it would be right below each message that has to be deleted?

                N Offline
                N Offline
                N a v a n e e t h
                wrote on last edited by
                #8

                Albert83 wrote:

                But there are other items in the DataList = the username of who posted, date, and message. That's why I chose the DataList to retrieve it from the database.

                I think you haven't got what I said. My point is to replace LinkButton with HyperLink button which will redirect to a particlular page and delete the supplied message. This is what CP is doing. You can see the Delete link points to a separate page which asks for the confirmation and delete it.

                Albert83 wrote:

                How would I use a hyperlink outside of the Data List so that it would be right below each message that has to be deleted?

                How come it will be right below of the each message if you put it outside of DataList? The LinkButton/HyperLink should be placed at the end of template column. So that it will come with each message. If you are creating some sort of forums, I believe repeater will be the right control than DataList. You will have more control over the rendered HTML.

                Navaneeth How to use google | Ask smart questions

                1 Reply Last reply
                0
                • N Not Active

                  If your application is properly architected then the user who posted the message will be linked to it through the id in your datasource. So if you pass the id of the message you lookup the name and compare it.


                  only two letters away from being an asset

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

                  so I pass the user name who logged in through the commandArgument?

                  N 1 Reply Last reply
                  0
                  • N Not Active

                    N a v a n e e t h wrote:

                    deletemessage.aspx?messageId=someid

                    Why?? Now you need another page, deletemessage.aspx, to handle what could be done in the same page, or passed to a controller and separated from your presentation.


                    only two letters away from being an asset

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

                    But I need to show the Delete text under the approprite message first and the process the delete. So if I use the hyperlink how would it show where needed?

                    N 1 Reply Last reply
                    0
                    • A Albert83

                      so I pass the user name who logged in through the commandArgument?

                      N Offline
                      N Offline
                      Not Active
                      wrote on last edited by
                      #11

                      Read the replys :rolleyes: Pass the message ID and lookup information about the message, like username, from your datasource.


                      only two letters away from being an asset

                      A 1 Reply Last reply
                      0
                      • N Not Active

                        N a v a n e e t h wrote:

                        deletemessage.aspx?messageId=someid

                        Why?? Now you need another page, deletemessage.aspx, to handle what could be done in the same page, or passed to a controller and separated from your presentation.


                        only two letters away from being an asset

                        N Offline
                        N Offline
                        N a v a n e e t h
                        wrote on last edited by
                        #12

                        You are right. I had a feeling that OP is absolute beginner and it would be tough for him if I suggest handling the DataList event and doing all the processing. I felt redirecting to another page would be trivial and easy for him to understand. This is the reason for that suggestion.

                        Navaneeth How to use google | Ask smart questions

                        A 1 Reply Last reply
                        0
                        • A Albert83

                          But I need to show the Delete text under the approprite message first and the process the delete. So if I use the hyperlink how would it show where needed?

                          N Offline
                          N Offline
                          Not Active
                          wrote on last edited by
                          #13

                          Use a template to display each row. I think this project is bit beyond your level.


                          only two letters away from being an asset

                          1 Reply Last reply
                          0
                          • N Not Active

                            Read the replys :rolleyes: Pass the message ID and lookup information about the message, like username, from your datasource.


                            only two letters away from being an asset

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

                            Ok. Thanks. I will try that.

                            1 Reply Last reply
                            0
                            • N N a v a n e e t h

                              You are right. I had a feeling that OP is absolute beginner and it would be tough for him if I suggest handling the DataList event and doing all the processing. I felt redirecting to another page would be trivial and easy for him to understand. This is the reason for that suggestion.

                              Navaneeth How to use google | Ask smart questions

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

                              Actually I am far from a beginner. I tried the data list handling as follows but it only gets the first row everytime, and starts over. it never goes to the next record. I know the problem but don't know the solution. What kind of template should I be using? Data List is a template. Thanks again. protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e) { foreach (DataListItem dlItem in DataList1.Items) { LinkButton lb = (LinkButton)dlItem.FindControl("deleteBtn"); using (SqlConnection sqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString)) { sqlConnection.Open(); SqlCommand cmdRFullName = new SqlCommand("SELECT username FROM mediaComments ", sqlConnection); SqlDataReader dr = cmdRFullName.ExecuteReader(); if (dr.HasRows) { while (dr.Read()) { if (Session["user_name"] != dr["username"]) lb.Visible = false; Response.Write("dr" + dr[0] + "
                              "); } } } }

                              <%# DataBinder.Eval(Container.DataItem, "commentText") %>

                              <%# String.Format("{0:hh}:{0:mm}{0:tt} {0:MM}.{0:dd}.{0:yy}", DataBinder.Eval(Container.DataItem, "datePosted"))%>

                              N 1 Reply Last reply
                              0
                              • A Albert83

                                Actually I am far from a beginner. I tried the data list handling as follows but it only gets the first row everytime, and starts over. it never goes to the next record. I know the problem but don't know the solution. What kind of template should I be using? Data List is a template. Thanks again. protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e) { foreach (DataListItem dlItem in DataList1.Items) { LinkButton lb = (LinkButton)dlItem.FindControl("deleteBtn"); using (SqlConnection sqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString)) { sqlConnection.Open(); SqlCommand cmdRFullName = new SqlCommand("SELECT username FROM mediaComments ", sqlConnection); SqlDataReader dr = cmdRFullName.ExecuteReader(); if (dr.HasRows) { while (dr.Read()) { if (Session["user_name"] != dr["username"]) lb.Visible = false; Response.Write("dr" + dr[0] + "
                                "); } } } }

                                <%# DataBinder.Eval(Container.DataItem, "commentText") %>

                                <%# String.Format("{0:hh}:{0:mm}{0:tt} {0:MM}.{0:dd}.{0:yy}", DataBinder.Eval(Container.DataItem, "datePosted"))%>

                                N Offline
                                N Offline
                                Not Active
                                wrote on last edited by
                                #16

                                Albert83 wrote:

                                Actually I am far from a beginner.

                                Not from your questions and responses

                                Albert83 wrote:

                                What kind of template should I be using?

                                See what I mean :laugh: :laugh:

                                Albert83 wrote:

                                ("SELECT username FROM mediaComments ",

                                in-line sql, another novice mistake

                                Albert83 wrote:

                                I know the problem

                                So do I, it's over your head. Try looking here[^] and reading about the events, like DeleteCommand and ItemDataBound


                                only two letters away from being an asset

                                A 1 Reply Last reply
                                0
                                • N Not Active

                                  Albert83 wrote:

                                  Actually I am far from a beginner.

                                  Not from your questions and responses

                                  Albert83 wrote:

                                  What kind of template should I be using?

                                  See what I mean :laugh: :laugh:

                                  Albert83 wrote:

                                  ("SELECT username FROM mediaComments ",

                                  in-line sql, another novice mistake

                                  Albert83 wrote:

                                  I know the problem

                                  So do I, it's over your head. Try looking here[^] and reading about the events, like DeleteCommand and ItemDataBound


                                  only two letters away from being an asset

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

                                  Ok. I am not an expert but have a lot of experience. What would be a better solution to that ? "SELECT username FROM mediaComments ", in-line sql, another novice mistake I looked at ItemDataBound before (Occurs when any button is clicked in the DataList control. ) The point is not to even show the delete button for the user who is not the poster of the comment. As to templates, Repeater, and Data List can be templated. If not please explain what you mean. Thanks.

                                  N 1 Reply Last reply
                                  0
                                  • A Albert83

                                    Ok. I am not an expert but have a lot of experience. What would be a better solution to that ? "SELECT username FROM mediaComments ", in-line sql, another novice mistake I looked at ItemDataBound before (Occurs when any button is clicked in the DataList control. ) The point is not to even show the delete button for the user who is not the poster of the comment. As to templates, Repeater, and Data List can be templated. If not please explain what you mean. Thanks.

                                    N Offline
                                    N Offline
                                    Not Active
                                    wrote on last edited by
                                    #18

                                    A better solution to in-line SQL? Ever heard of stored procedures? :rolleyes:

                                    Albert83 wrote:

                                    I looked at ItemDataBound before (Occurs when any button is clicked in the DataList control. )

                                    Really? Then look again "Occurs when an item is data bound to the DataList control."[^] You are using template, but using the methods incorrectly.

                                    Albert83 wrote

                                    "Response.Write("dr" + dr[0] + "<br />");"

                                    ; You don't need this. Use the template to show your items, that's what it there for. You should have enough information now, I suggest you read a little more and look at some of the many examples available. If you need any more, I'll have to start charging you. Where should I send the invoice?


                                    only two letters away from being an asset

                                    A N 2 Replies Last reply
                                    0
                                    • N Not Active

                                      A better solution to in-line SQL? Ever heard of stored procedures? :rolleyes:

                                      Albert83 wrote:

                                      I looked at ItemDataBound before (Occurs when any button is clicked in the DataList control. )

                                      Really? Then look again "Occurs when an item is data bound to the DataList control."[^] You are using template, but using the methods incorrectly.

                                      Albert83 wrote

                                      "Response.Write("dr" + dr[0] + "<br />");"

                                      ; You don't need this. Use the template to show your items, that's what it there for. You should have enough information now, I suggest you read a little more and look at some of the many examples available. If you need any more, I'll have to start charging you. Where should I send the invoice?


                                      only two letters away from being an asset

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

                                      Sure I heard of stored procedures, I use them when I find necessary. "Response.Write("dr" + dr[0] + "
                                      ");" is for testing purposes. A lot of programmers do it when testing the results of the query. All the items are templated actually. You are right i mixed it with itemCreated. Thanks for the help.

                                      N 1 Reply Last reply
                                      0
                                      • N Not Active

                                        A better solution to in-line SQL? Ever heard of stored procedures? :rolleyes:

                                        Albert83 wrote:

                                        I looked at ItemDataBound before (Occurs when any button is clicked in the DataList control. )

                                        Really? Then look again "Occurs when an item is data bound to the DataList control."[^] You are using template, but using the methods incorrectly.

                                        Albert83 wrote

                                        "Response.Write("dr" + dr[0] + "<br />");"

                                        ; You don't need this. Use the template to show your items, that's what it there for. You should have enough information now, I suggest you read a little more and look at some of the many examples available. If you need any more, I'll have to start charging you. Where should I send the invoice?


                                        only two letters away from being an asset

                                        N Offline
                                        N Offline
                                        N a v a n e e t h
                                        wrote on last edited by
                                        #20

                                        Mark Nischalke wrote:

                                        A better solution to in-line SQL? Ever heard of stored procedures?

                                        It's a myth. IMO, nothing wrong with inline SQL other than the difficulty of recompiling code when the DB structure changes. This can be avoided by abstracting the table structure using views. So still you can write inline SQLs safely. Please do correct me if you felt I am wrong.

                                        Navaneeth How to use google | Ask smart questions

                                        N 1 Reply Last reply
                                        0
                                        • N N a v a n e e t h

                                          Mark Nischalke wrote:

                                          A better solution to in-line SQL? Ever heard of stored procedures?

                                          It's a myth. IMO, nothing wrong with inline SQL other than the difficulty of recompiling code when the DB structure changes. This can be avoided by abstracting the table structure using views. So still you can write inline SQLs safely. Please do correct me if you felt I am wrong.

                                          Navaneeth How to use google | Ask smart questions

                                          N Offline
                                          N Offline
                                          Not Active
                                          wrote on last edited by
                                          #21

                                          N a v a n e e t h wrote:

                                          Please do correct me if you felt I am wrong.

                                          Where do I start? This argument has been raised several times. Inline doesn't allow for separation of the business logic from the presentations, which a good design does, and increases maintenance and development. 10 apps with the same SQL statement must each be changed, rather changing one stored proc. Worse yet, if the schema changes, say a table or column is renamed, all statements must be changed instead of changing one stored proc. Inline statements also don't allow the RDBMS to optimize the execution or otherwise cache it for better performance. Just few of the many reasons. Then again, if you don't care about robust, well designed and performant applications, keep using inline statements.


                                          only two letters away from being an asset

                                          A 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