?
Solved

Gridview LinkButton Click display details in another column asp.net and c#. Please check the attachments

Posted on 2012-08-23
9
Medium Priority
?
4,548 Views
Last Modified: 2013-01-27
Doc1.docI have a gridview having two columns ScriptName and ScriptText. On Page Load, I am displaying the script names as linkbuttons.

On click of linkbutton displays the details in scripttext column and change the color of linkbutton. Scripttext column is just a one row column.

Please see my code and this code will display layout1 (doc1). i want layout2(Final Layout).

Thanks

 WebPage.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="scriptorderTest.aspx.cs" Inherits="scriptorderTest" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form runat="server">
       <asp:GridView ID="ScriptsGridView" runat="server" AutoGenerateColumns="False" 
                    onrowdatabound="ScriptsGridView_RowDataBound" >
        <HeaderStyle BackColor="#F28B13" ForeColor="White"></HeaderStyle>
        <RowStyle Height="20px" />
         <Columns>

             <asp:TemplateField >  

       <ItemTemplate>  
              <asp:HiddenField ID="hdnid" runat="server" Visible="true" Value='<%# Bind("ScriptID") %>' />  
              
       </ItemTemplate>  
         </asp:TemplateField>
            <asp:TemplateField HeaderText="Script Name">  
                <ItemTemplate> 
                    <asp:LinkButton ID="UserNameLink"   runat="server" Text='<%# Eval("Title") %>'  OnClick="UserNameLink_Click" />
                 </ItemTemplate>  
                <ItemStyle Width="40%" BorderColor="#CCCCCC" />
            </asp:TemplateField>  
    
            <asp:TemplateField HeaderText="ScriptText" > 
            
                <ItemTemplate>   
                  <table><tr><td><asp:TextBox runat="server" ID="txtb1" ></asp:TextBox></td></tr></table>  
                </ItemTemplate>  
                <ItemStyle Width="60%" BorderColor="#CCCCCC" />
            </asp:TemplateField>  
           
      </Columns>
     </asp:GridView>
          
    <asp:ObjectDataSource ID="ApplicationSystemDataSource" runat="server" SelectMethod="GetSystems"
        TypeName="Script"></asp:ObjectDataSource>
    <asp:ObjectDataSource ID="ProductDataSource" runat="server" SelectMethod="GetAllProdGroupsAsCollection"
        TypeName="ProductGroup"></asp:ObjectDataSource>
    </form>

//webpage.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Web.Services;
using System.Data;
using System.Configuration;
using System.Web.SessionState;


public partial class scriptorderTest : System.Web.UI.Page
{
    private string connectionString = ConfigurationManager.ConnectionStrings["DBConnect"].ConnectionString;
        
  protected void Page_Load(object sender, EventArgs e)
    {
        SqlDataReader myDataReader;
        Script objScript = new Script();
        myDataReader = objScript.GetScriptsOrderBySelection(2, 1000, 2000);
        if (myDataReader.HasRows)
        {
            this.ScriptsGridView.DataSource = myDataReader;
            ScriptsGridView.DataBind();

            objScript = null;

        }
    }

    public SqlDataReader GetScriptsOrderBySelection(int iSystem_ID, int iProductGroup_ID, int iSubProductGroup_ID)
    {
        SqlConnection objConnect = new SqlConnection(connectionString);
        SqlCommand objCmd = new SqlCommand("usp_GetSelectedScriptsForAdmin", objConnect);
        objCmd.CommandType = CommandType.StoredProcedure;
        objCmd.Parameters.Add(new SqlParameter("@ApplicationSystem_ID", SqlDbType.Int)).Value = iSystem_ID;
        objCmd.Parameters.Add(new SqlParameter("@ProductGroup_ID", SqlDbType.Int)).Value = iProductGroup_ID;
        objCmd.Parameters.Add(new SqlParameter("@SubProductGroup_ID", SqlDbType.Int)).Value = iSubProductGroup_ID;

        try
        {
            // Open connection to the database
            objConnect.Open();

            // Execute the stored proc to initialize the DataReader
            // Connection will be closed when DataReader goes out of scope
            return objCmd.ExecuteReader(CommandBehavior.CloseConnection);
        }

        catch (Exception objErr)
        {
            throw (objErr);
            // Logg the exception here
        }


    }


    protected void ScriptsGridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        if (e.Row.RowType == DataControlRowType.DataRow)
        {


        }
    }
    protected void UserNameLink_Click(object sender, EventArgs e)
    {
        GridViewRow clickedRow = ((LinkButton)sender).NamingContainer as GridViewRow;
        HiddenField lblID = (HiddenField)clickedRow.FindControl("hdnid");
        string s = lblID.Value;
        int s1 = Convert.ToInt32(s);

        string sqlSelectKeysFromMasterTbl = "";
        sqlSelectKeysFromMasterTbl = "SELECT * FROM [dbo].[ScriptText] WHERE Script_ID = '" + s1 + "'  and ScriptTextPlain != '' ";
        SqlConnection conn = new SqlConnection(connectionString);
        
        conn.Open();                
        SqlCommand selectCmd = new SqlCommand(sqlSelectKeysFromMasterTbl, conn);

        SqlDataReader dataReader;
        dataReader = selectCmd.ExecuteReader();
        // Always call Read before accessing data.
        if (!dataReader.Read())
        {
            // Need read once for one record. If it fails means record does not exist. 
            //And we cant proced further. throw error
            dataReader.Close();
            conn.Close();
           
            return;
        }

        string iBankerId = dataReader.GetString(2);
        TextBox tb = (TextBox)clickedRow.FindControl("txtb1");
        tb.Text = dataReader.GetString(2);
      //  int iQAOfficerId = dataReader.GetInt32(1);

        // always call Close when done reading.
        dataReader.Close();

    }

   
    public SqlDataReader GetScriptsText(int iScript_ID)
    {
        SqlConnection objConnect = new SqlConnection(connectionString);
        SqlCommand objCmd = new SqlCommand("usp_GetSelectedScriptsText", objConnect);
        objCmd.CommandType = CommandType.StoredProcedure;
        objCmd.Parameters.Add(new SqlParameter("@Script_ID", SqlDbType.Int)).Value = iScript_ID;
       
        try
        {
            // Open connection to the database
            objConnect.Open();

            // Execute the stored proc to initialize the DataReader
            // Connection will be closed when DataReader goes out of scope
            return objCmd.ExecuteReader(CommandBehavior.CloseConnection);
        }

        catch (Exception objErr)
        {
            throw (objErr);
            // Logg the exception here
        }


    }

}
   
</body>
</html>

Open in new window

FinalLayout.doc
0
Comment
Question by:Saroj13
8 Comments
 
LVL 20

Expert Comment

by:informaniac
ID: 38328521
Is it required for you to display the scripttext inside the gridview itself? Can't it be say inside a label that resides inside a panel.
0
 
LVL 26

Accepted Solution

by:
Alan Warren earned 2000 total points
ID: 38328870
Hi

You can hook up a detailsview to a GridView.SelectedValue by assigning a ControlParameter ControlID="gv" PropertyName="SelectedValue" to the detailsView Datasource.

Then the detailsView will follow the GridView.

<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <table width="400px">
        <tr style="vertical-align:top;">
        <td style="border:1px solid #000;">
         <asp:GridView ID="gv" runat="server" AutoGenerateColumns="False" Width="100%" Height="100%" 
        DataKeyNames="ScriptID" DataSourceID="SomeSqlDataSource" CellPadding="4" 
           ForeColor="#333333" GridLines="None">
        <Columns>
          <asp:TemplateField>
            <ItemTemplate>
              <asp:linkbutton ID="TitleLinkButton" runat="server" Text='<%# Eval("Title") %>' CommandName="Select" />
             </ItemTemplate>          
          </asp:TemplateField>
        </Columns>
      </asp:GridView>
        
        </td>
        <td style="border:1px solid #000;">
          <asp:DetailsView ID="DetailsView1" runat="server" Height="100%" Width="100%"  BorderStyle="None" FieldHeaderStyle-BorderStyle="None" 
            AutoGenerateRows="False" DataKeyNames="ScriptID" DataSourceID="SomeOtherSqlDataSource" >
            <Fields>
              <asp:TemplateField ItemStyle-BorderStyle="None">
                <ItemTemplate>
                  <asp:Label ID="DetailLabel" runat="server" Text='<%# Eval("Detail") %>' BorderStyle="None" />
                 </ItemTemplate>          
              </asp:TemplateField>
              
            </Fields>

          </asp:DetailsView>
        </td>
        </tr>
      </table>
    
      <asp:SqlDataSource 
          ID="SomeSqlDataSource" runat="server" 
          ConnectionString="<%$ ConnectionStrings:cn %>" 
          SelectCommand="SELECT [ScriptID], [Title] FROM [Table_2]"
          >
      </asp:SqlDataSource>

      <asp:SqlDataSource 
          ID="SomeOtherSqlDataSource" runat="server" 
          ConnectionString="<%$ ConnectionStrings:cn %>" 
          SelectCommand="Table_2_Detail_Sel"
          SelectCommandType="StoredProcedure"
          >
      <SelectParameters>
        <asp:ControlParameter ControlID="gv" PropertyName="SelectedValue" Name="ScriptID" Type="Int32" DefaultValue="1" />
      </SelectParameters>
      </asp:SqlDataSource>
    
    </div>
    </form>
</body>
</html>

Open in new window

detailsview synched to gridview selectedvalue image
0
 

Author Comment

by:Saroj13
ID: 38338444
Hi Alan,

would you please tell me what is
Table_2_Detail_Sel in
  <asp:SqlDataSource
          ID="SomeOtherSqlDataSource" runat="server"
          ConnectionString="<%$ ConnectionStrings:cn %>"
          SelectCommand="Table_2_Detail_Sel"
          SelectCommandType="StoredProcedure"
          >
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 26

Expert Comment

by:Alan Warren
ID: 38338834
The name of the the stored procedure I created for testing, you should probably replace that with the name of your stored procedure.

Alan ";0)
0
 

Author Comment

by:Saroj13
ID: 38341975
Thanks. I have another question.
If gridview is empty, then how to display
header ScriptTitle and ScriptDetail with one empty row.

If gridview is not having any rows, then i want detailsview to be also empty.
Both gridview and details view have the same height.
0
 

Author Comment

by:Saroj13
ID: 38406825
1. How to get rid of first empty row from Title Column?
2. How to add header "title" and text in both the columns?
3. On page load, no title link is selected. That means no detail is displayed on page load
4. On link click, it will display the details. How to change the color of selected title link? Only selected link color will change.

Please let me know how we can do this?

Thanks
0
 

Author Comment

by:Saroj13
ID: 38407085
First two problems are resolved.

3. On page load, no title link is selected. That means no detail is displayed on page load
4. On link click, it will display the details. How to change the color of selected title link? Only selected link color will change.

Please let me know how we can do this?
0
 
LVL 19

Expert Comment

by:Amandeep Singh Bhullar
ID: 38823906
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question