Solved

Gridview Databound Control

Posted on 2007-11-30
4
3,010 Views
Last Modified: 2013-11-26
I have a Gridview with databound columns.  When the Gridview is displayed the headers for the databound columns headers appear, along with any data, but the textbox controls do not. The controls seem to be created dynamically when the user selects the edit button.

I am looking for a way to shorten the width of several of these dynamically created textboxes.  

Does anyone have a clue as to how this could be done?
0
Comment
Question by:ExecQ
4 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 20383976
Does the column have an EditItemTemplate defined?

Bob
0
 
LVL 16

Expert Comment

by:SQL_SERVER_DBA
ID: 20384476
make sure your table has and index or primary key of somesort, refresh the grid...
0
 
LVL 10

Accepted Solution

by:
digitalZo earned 250 total points
ID: 20387144
You have to use EditItemTemplate for that.
 <asp:templatefield headertext="Field1">
 
        <itemstyle width=60% wrap=True />
 
        <itemtemplate>
           <asp:label runat="server" 
              id="Field1"
              text='<%# Eval ( "Field1" ) %>' />
        </itemtemplate>
 
        <edititemtemplate>
           <asp:textbox textmode="singleline" runat="server" MaxLength=2
              id="Field1" rows=5 width=40%
              text='<%# Bind ( "Field1" ) %>'  />
        </edititemtemplate>
        
     </asp:templatefield>
       

Open in new window

0
 

Author Comment

by:ExecQ
ID: 20397182
Yes, the column does have a EditTemplate Field.

<asp:GridView id="gv_dc" runat="server" OnDataBound="gv_dc_DataBound" DataKeyNames="ClassID,StudentID,ClassMonth,ClassPeriodID" DataSourceID="sql_contactHours" CellPadding="3" BorderWidth="1px" BorderStyle="Dotted" BackColor="White" AllowSorting="True" Width="798px" AllowPaging="True">
<Columns>
    <asp:TemplateField ShowHeader="False">    
<EditItemTemplate>
    <asp:Button id="Button1" runat="server" Text="Update" CommandName="Update" CausesValidation="True"></asp:Button><br />
    <asp:Button id="Button2" runat="server" Text="Cancel" CommandName="Cancel" CausesValidation="False"></asp:Button>
</EditItemTemplate>
<ItemTemplate>
    <asp:Button id="Button1" runat="server" Text="Edit" CommandName="Edit" CausesValidation="False" Width="39px"></asp:Button>
</ItemTemplate>      
    </asp:TemplateField>
</Columns>
<HeaderStyle CssClass="font_tab"></HeaderStyle>
</asp:GridView>

The table does have a primary key field and index, but it also is created dynamically.

Here is code to create the textboxes when the grid loads:

System.Web.UI.WebControls.TextBox tb = new System.Web.UI.WebControls.TextBox();
tb.ID = "day_" + ClassDays[i].ToString();
tb.ControlStyle.Width = 50;
tb.Width = 5;

Neither of the width properties work.  This code is placed within a for loop that runs after the sql statement has been formulated.  It provides the grid with the number of columns, depending on the sql statement.  There are no set number of columns at anytime for the grid, so I don't think I can specify an edittemplate in the way digitalZo is commenting...
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

786 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