[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 350
  • Last Modified:

How can I format cells of a Detail View alignment?

Hi, I'm using asp.net 3.5, C#
I have a Detail View control that I assign sqldatasource at runtime because I need to get the parameter for sql query from session variable only at runtime.  Some fields needs to be left aligned while others need to be either center or right aligned.  How can I specified this at runtime?  Thank you.
0
lapucca
Asked:
lapucca
3 Solutions
 
sonawanekiranCommented:
You can pass the session parameter using SessionParameter like below
<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:testDatabaseConnectionString %>" 
        SelectCommand="SELECT * FROM [UserTable] WHERE ([userID] = @UserID)">
        <SelectParameters>
            <asp:SessionParameter Name="UserID" SessionField="UserID" Type="Int32" />
        </SelectParameters>
    </asp:SqlDataSource>

Open in new window

0
 
sonawanekiranCommented:
For alignment show your detailview code in aspx file
0
 
Kumaraswamy RCommented:
HI


example

<asp:GridView ID="GridView1" runat="server" OnRowCreated="OnRowCreated" cellpadding="5">
    <columns>
        <asp:boundfield datafield="Name" headerstyle-forecolor=Blue />
    </columns>
</asp:GridView>


protected void OnRowCreated(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.Header)
    {
        Int32 idx = 0;
        foreach(TableCell cell in e.Row.Cells)
        {
            cell.Text = String.Format("{0}_{1}", cell.Text, (++idx));
            if (idx%2 == 0)
            {
                cell.HorizontalAlign = HorizontalAlign.Right;
            }
            else
            {
                       cell.HorizontalAlign = HorizontalAlign.Left;
            }
        }
    }
}            
0
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!

 
lapuccaAuthor Commented:
Sona,
Thank you for showing me how to setup sqldatasource in aspx using asp:sessionparameter.  Attached are the aspx and the cs files.  Also, aI noticed that all fiedls are left aligned in Firefox but in IE they're all centered which comes form the <center> in aspx file.  Wonder why they look different in different browsers.  Thanks.
Implementation.aspx
Implementation.aspx.cs
0
 
lapuccaAuthor Commented:
Also, is there a way to set padding for all cells in the DetailView control?  Thanks.
0
 
sonawanekiranCommented:
It seem your are adding all columns (from select statement) to details view. In order to different alignment for different columns you need to add the BoundFields to data column for instance look below code. There is item style with horizontal align

<asp:DetailsView ID="dvImplementation" runat="server" Width="600px" CommandRowStyle-VerticalAlign="Middle" HeaderStyle-BackColor="#99FF66" BackColor="White" BorderColor="Black" BorderStyle="Solid">
    <Fields>
        <asp:BoundField DataField="jKeepGoal" HeaderText="Keep Goal">
             <itemstyle HorizontalAlign="Center">
              </itemstyle>
         </asp:boundfield>
        <asp:BoundField DataField="lBreakfast" HeaderText="Break fast" >
             <itemstyle HorizontalAlign="Left">
              </itemstyle>
         </asp:Boundfield>
       
    </Fields>
</asp:DetailsView>

For padding you can use cellpadding property of detailview

cellpadding="10"
0
 
ExpertLogin_890Commented:
Hi,

For padding a cells you can set detailview control cell padding property like
cellpadding ="4" cellspacing="5", etc

for dynamically alignment the cells use the detailsview control onrowcreated event set the any routine to perform the alignment of cells check the below links

http://it.toolbox.com/blogs/codesharp/detailsview-on-steroids-20231

0

Featured Post

Industry Leaders: 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!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now