locked
Want only 50 characters in a field showing up RRS feed

  • Question

  • User-643157890 posted

    I design a Gridview to showing a list of records. The user can click on Select to go to a specific record. The Summary field in this Gridview has a lot of wordings that we want to cut off to show only 50 characters. I want to show it fully in the Details page only.

    I use Substring([Summary], 1, 50) in the configuration data source of the gridview. It works in the gridview list; however, when I go to the Details page, the Summary field show only 50 characters.

    There is a way to work around this issue by calling a stored procedure, but I don't want to do that. Do you have an efficient way to fix it so that the Detail page will show the Summary field fully? Thank you in advance for your help.   

    Wednesday, August 17, 2016 4:12 PM

Answers

  • User-707554951 posted

    Hi newUser15:

    From your description, Since, the GridView in Master page, if in the details page, if you want to update the page content according to the GridView selected row. I suggest you could get the selected row’s primary key value on the Gridview SelectedIndexChanged event and  you could use findcontrol() method to find DetailsView(in detailpage) in ContentPlaceHolder In materpage. Then you could rebind data to details view.

    Following code  may be helpful to you , you could refer to it:

    <p>Master pages  </p>
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1"  OnSelectedIndexChanged="GridView1_SelectedIndexChanged"
                AutoGenerateSelectButton="True" DataKeyNames="CategoryID">
                <Columns>
                    <asp:BoundField DataField="CategoryID" HeaderText="Category ID" />
                     <asp:BoundField DataField="CategoryName" HeaderText="Category Name" />
                </Columns>
            </asp:GridView>       
             <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
                 SelectCommand="SELECT [CategoryID], SUBSTRING([CategoryName], 1, 3) AS CategoryName FROM [Categories]"></asp:SqlDataSource>
            
             <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">        
            </asp:ContentPlaceHolder>
    

    CodeBehind:

    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
            {
                string id = GridView1.SelectedRow.Cells[1].Text;
    
               System.Web.UI.WebControls.DetailsView DetailsView1 = (System.Web.UI.WebControls.DetailsView)ContentPlaceHolder1.FindControl("DetailsView1");
                if (DetailsView1 != null) { 
                    string query = "select * from Categories where CategoryID=@CategoryID";
                string conString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
    
                using (SqlConnection con = new SqlConnection(conString))
                {
                    using (SqlCommand cmd = new SqlCommand(query, con))
                    {
                        con.Open();
                        cmd.Parameters.AddWithValue("@CategoryID", id);
                        var results = cmd.ExecuteReader();
                        DetailsView1.DataSource = results;
                        DetailsView1.DataBind();
                        con.Close();
                    }
    
                }
                }       
    
        }
    

    DetailPage:

    <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
        <asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="125px"></asp:DetailsView>
    </asp:Content>

    Hope this can help you. If you have any question and confusion about the problem. Please don't hesitate to let me know.

    Best regards
    Cathy

     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 18, 2016 12:46 PM

All replies

  • User2053451246 posted

    The Details page you speak of should have it's own data source.  Don't include Substring([Summary], 1, 50) in that SQL statement?

    Wednesday, August 17, 2016 5:21 PM
  • User-643157890 posted

    ryanbesko,

    Thank you for replying. No, I don't include Substring([Summary], 1, 50) in the SQL statement in the Details page. I use Session["Summary"].ToString() to hold the value of Summary and then in the Details page, I assign it to the Summary text field. In the other words, the Summary value from the Master page is passed to the Details page. My problem is I want to show only 50 characters in the Master page, but in the Details page I want to show it fully.

    Thanks

    Wednesday, August 17, 2016 6:21 PM
  • User-183374066 posted

    Hi,

    You can do trim the characters on client side. Just fetch full summary on your master page. Trim the text by following by jquery

    $(function(){
      if($('#myTextBox').val().length > 50){
      	$('#myTextBox').val($('#myTextBox').val().substring(0,50))
      }
    });

    see example

    Wednesday, August 17, 2016 7:31 PM
  • User2053451246 posted

    I use Session["Summary"].ToString() to hold the value of Summary and then in the Details page

    That's an odd way of doing that.  You have two options:

    Use another Session variable that has all the characters, or load the data from the database when you show the details page.

    Wednesday, August 17, 2016 8:35 PM
  • User-707554951 posted

    Hi newUser15:

    From your description, Since, the GridView in Master page, if in the details page, if you want to update the page content according to the GridView selected row. I suggest you could get the selected row’s primary key value on the Gridview SelectedIndexChanged event and  you could use findcontrol() method to find DetailsView(in detailpage) in ContentPlaceHolder In materpage. Then you could rebind data to details view.

    Following code  may be helpful to you , you could refer to it:

    <p>Master pages  </p>
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1"  OnSelectedIndexChanged="GridView1_SelectedIndexChanged"
                AutoGenerateSelectButton="True" DataKeyNames="CategoryID">
                <Columns>
                    <asp:BoundField DataField="CategoryID" HeaderText="Category ID" />
                     <asp:BoundField DataField="CategoryName" HeaderText="Category Name" />
                </Columns>
            </asp:GridView>       
             <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
                 SelectCommand="SELECT [CategoryID], SUBSTRING([CategoryName], 1, 3) AS CategoryName FROM [Categories]"></asp:SqlDataSource>
            
             <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">        
            </asp:ContentPlaceHolder>
    

    CodeBehind:

    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
            {
                string id = GridView1.SelectedRow.Cells[1].Text;
    
               System.Web.UI.WebControls.DetailsView DetailsView1 = (System.Web.UI.WebControls.DetailsView)ContentPlaceHolder1.FindControl("DetailsView1");
                if (DetailsView1 != null) { 
                    string query = "select * from Categories where CategoryID=@CategoryID";
                string conString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
    
                using (SqlConnection con = new SqlConnection(conString))
                {
                    using (SqlCommand cmd = new SqlCommand(query, con))
                    {
                        con.Open();
                        cmd.Parameters.AddWithValue("@CategoryID", id);
                        var results = cmd.ExecuteReader();
                        DetailsView1.DataSource = results;
                        DetailsView1.DataBind();
                        con.Close();
                    }
    
                }
                }       
    
        }
    

    DetailPage:

    <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
        <asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="125px"></asp:DetailsView>
    </asp:Content>

    Hope this can help you. If you have any question and confusion about the problem. Please don't hesitate to let me know.

    Best regards
    Cathy

     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 18, 2016 12:46 PM
  • User-643157890 posted

    Thanks a lot, Cathy. Your solution works.

    Tuesday, August 23, 2016 1:10 PM