how to get a dataitem's column name in a custom databound control

 I know how to get the value, but not its associated column name... (ie. "Name")

0
notinmyname
5/20/2008 2:35:08 PM
asp.net.presentation-controls 72751 articles. 3 followers. Follow

1 Replies
556 Views

Similar Articles

[PageSpeed] 57

Hi notinmyname ,

I hope my sample is helpful to you.

 

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DataRowView drv =  e.Row.DataItem as DataRowView;
            for (int i = 0; i < drv.DataView.Table.Columns.Count; i++)
            {
                Response.Write(drv.DataView.Table.Columns[i].ColumnName);
            }
        }
    }
  
    <form id="form1" runat="server">
    <div>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString2 %>"
            SelectCommand="SELECT * FROM [country]"></asp:SqlDataSource>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="countryid"
            DataSourceID="SqlDataSource1" OnRowDataBound="GridView1_RowDataBound">
            <Columns>
                <asp:BoundField DataField="countryid" HeaderText="countryid" ReadOnly="True" SortExpression="countryid" />
                <asp:BoundField DataField="countryname" HeaderText="countryname" SortExpression="countryname" />
            </Columns>
        </asp:GridView>
    
    </div>
    </form>
 

Samu Zhang
Microsoft Online Community Support

Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question.
0
Samu
5/22/2008 7:10:46 AM
Reply:

Similar Artilces:

how to get a control ID from a bind field name / bound field (column) name from a formview's control
I have a form view which is bound to a table, now from codebehind file I need to apply some formatting based on some of the field's bound control. For example, if Formview1 is bound to Employee table, and it's item template has a control with ID "Label1" which is bound to "EmployeeJoinDate" . Now, I have a different table which has a list of field name(EmployeeJoinDate is one of them), all of which should be applied some formatting based on some condition. Now I can take two approach for that, but I am stuck at both:  1. If I can somehow get the contro...

Getting 'System.Web.UI.Control' does not contain a definition for 'DataItem' error on controls when placed inside an update panel which is in a template.
Using the June CTP, I;ve got an updatepanel inside the itemtemplate of a datalist which is now suppopsed to be supported (it works). I also have some asp controls (imagebutton, label) which get their values dynamically (DataBinder.Eval(Container.DataItem, "fieldname").ToString(). This would work fine before putting the updatepanel in the datalist itemtemplate, I now get the error in the title when building ('System.Web.UI.Control' does not contain a definition for 'DataItem') for each control... How can I dynamically assign the values to the controls in the datalist when also using upd...

Unable to access user control's user control's function\property from another user control
Hi, I used to call an user control's user control function as stated below from my user control in ASP.Net 1.1 wucCompany.wucEmployee.GetEmployeeSomething() After migrating to ASP.Net 2.0, I am unable to use any properties/functions(even the public ones) of the user control's User control's from another user control.  The way I have to do is create property\function in wucCompany which calls the wucEmployee's property\function and call the wucDepartment.GetEmployeeSomething().Since I need to do this change in too many places, I can...

Getting User Control's Control Data
I am building a wizard that will pull opt reports.  Page 1 let's you pick a website you want to work with based on listings in a table.  Page 2 displays which options you can include in your opt report by bit fields in the same table that fills the dropdown on Page1.  Based on which fields get  a 1, user controls are loaded to display the options.  My question is this: How can I get the information on the options the user selects from the individual user controls? I've read up on serialization but I don't know how to capture an event that fires when a page submits a...

how to get a control's name
 Hi  In a webform has some controls,i want to get the control's name to do other things  How can i get a control's name,such as  DropDownlist  Thanks a lot  This snippet loops through the controls in a webform and gives you the control type name. foreach (Control cntrl in form1.Controls) { Response.Write(cntrl.GetType().Name.ToString()); }   How to write the code use vb.netThanks a lot Mark Lokira's answer as the answer.  For Each cntrl As Control In form1.Controls Response.Write(cntrl.[GetType]().Name.ToString...

Selecting another control in a custom control's properties
I have a custom control I'm creating and It is going to influence a gridview on the parent page.  What I would like is when the custom control is dropped onto the page in the designer, to be able to select the gridview in the custom control's properties (similar to the way when you drop a RequiredFieldValidator you can select other controls in the ControlToValidate property.  Is there a way to mimick the ControlToValidate property, except be able to select from any GridViews on the page?   Thanks! Hi, first you need to create a type converter. You can creat...

Setting a panel control's visibility and it's affect in nested controls.
I am trying to debug a page using the panel control.  The code in the final submission of the form has logic to get data from a dropdown based on the control's visibility.  If the control's visibility is set to true, the the data is retrieved and sent to the database.The problem appears to be the dropdown control's visibility is set to false when it should not.  The dropdown is nested in a panel control.  While doing a trace, it appears when a panel control's visibility is set to false, the visibility for all controls nested under this panel are set to false as well.You'r...

Getting the recreated control's id's by viewstate?
Hi How can i get the automaticly created fileupload control's id's by stroring them in the viewstate? Is it possible? or any other way to get them on the run-time? I can store button's click number and then create dynamic controls on the run-time and if i look at the source code i can see the auto-created id's but when i click submit button to upload files in fileupload controls i get an NullReferenceException. I guess when i click submit button all the id's gone because the page posts back. So i write this to read id's and assign to my fileupload control, here is the code for it...

Rename Custom Controls DLL's or packed into one control
Hello I have some Custom Controls DLL's and i need to rename this file. We don't have the SourceCode. Or it's possible to packed (join) them all in to one Assembly? Thank Reiner Max...

Ajax Control Toolkit: Control cannot be created because Visual Studio cannot find the control's type in the control assembly.
Hi All, Good Day.  I am using ASP.Net 2.0 Framework, i have downloaded Ajax 3.0 and added reference into the project. But i am not able to add controls to the page. When i tried to add control into the page getting following error message "Control cannot be created because Visual Studio cannot find the control's type in the control assembly". I checked some forums on the web, added references in Web.Config. Still not able to proceed further. Please revert asap.  Thanks. AJAX 3.0 not work in 2.0 Framework its work in 3.5 Framework try to download another ...

Can I hide a user control's control from the hosting page's client script?
Okay, my previous question was not unswered and I guess you can't hide a control's panel with server code then show it with client code. Now this is what I try to do: on the hosting page's onload event, call a function to hide the user control's penal. Here is the deal: the user control was looped as many times as data required. So the penals will be name mypanel1, my panel2, etc... that is not even a problem if I know if the following is true: * Do all server codes finish running before the page get sent back to the client? Say the database gave me 5 penals, I can then determine how...

when i load control dynamically i cannot get server control's clientid in javascript
i load control dynamically from codebehind, in one placeholder,i put javascript  validations for pages,but i cannot get servercontrol's clientid in javascript function, also when i see the "view source" of page, i cannot find those servercontrols which load dynamically, is there any solution to do this??thanks VISHAL PAREKH  When you load your control, please add an ID on it ex:TextBox txt = new TextBox();txt.ID = "MyID";Then on your script you can point to this txt.ClientIDHope this helpCheersAndyRemember to mark this as answered if this solved ...

Label control's text doesn't appear to be set (in the page which has a user control )when trying to update the text property in the FormView's ItemUpdated event
Hi There,I have a user control which hosts a formview control, gridview control and a label control. The grid view is usedto show rows in the DB and when clicked to edit each row, a form view opens up in edit mode with that particular row info. So I have nothing in the item template.My first question is..is it mandatory to have smth in the itemtemplate of the form view.I have controls in edititemtemplate and insertitemtemplate.When a particular row is edited, I would like to show a status msg showing that the row has been updated.For this, I use the ItemUpdated event of the formview and try ...

Custom composite contorl contains custom contols: controls collection gets empty (Or control get reinitialized) on postback.
Hi, I'm  developing a ASP .Net custom server control library. Where all the basic(Text,check,etc) controls are working well. I have developed an composite control which is more like a Tabular control with the basic custom controls in every cell. The problem is, this composite control reinitializes on post back. I 've added the custom controls (in CreateChildControls method) as cell.controls.add(customcontrol)  row.controls.add(cell) ; this.controls.add(row) Please give me a solution as early as possible IMXO. ASP.NET constructs and reinitializes&n...

Web resources about - how to get a dataitem's column name in a custom databound control - asp.net.presentation-controls

Resources last updated: 12/5/2015 5:00:38 PM