I have an question, On my page i have a DataView control, I also have a button that has CommandArgument. I know i can read the DataView as :
myDataView.Rows[i].FindControl("FaqQuestion");
I want to add index value in runtime to the CommantParameter, so when i go to the Function onCommand i will know exactly from what row[i] i Need to get my controls from DataView.
So my question is, how do i dinamicly add index of DataView.Rows[i] into CommmandArgument for the Button in runtime ?
Thanks in advance.
Implement the OnDataBinding event for the button. In the event, assign the CommandArguemnt value to whatever you want. Normally you would assign an ID or some other identifier.
<ItemTemplate>
<%# Container.ItemIndex %>
</ItemTemplate>
Related
Okay I researched this as much as possible before I posted so I hope this is not a repost but here goes...
I made a datatable, lets say it has a coulmn called "cartype" , then I added a column called "color"
well, I bound a gridview what I want to do is use a label, and have its FORECOLOR be the VALUE in the column "color".
I tried this:
<asp:BoundField DataField="cartype" HeaderText="Cars" ItemStyle-Width="130" ItemStyle-ForeColor='<% Eval("Color") %>' />
but I got an error about
"Cannot create an object of type System.drawing.color from its string representation '<%Eval("color")%>' for the 'ForeColor' Property.
I also tried adding a template field and got the same result.
I was trying not to use the rowdatabound event and use .Cells[3] , because then when I add columns, it's going to change the cell number, and switch up everything! I was hoping I could make it cleaner by binding the color with the data.
Okay I ended up doing this. sad :(
At the VERY END of my grid I added an ASP:Hidden template field
<asp:TemplateField>
<ItemTemplate>
<asp:HiddenField ID="hColor" runat="server" Value='<%# Eval("Color") %>'/>
</ItemTemplate>
</asp:TemplateField>
Then in the RowDataBoundEvent I did this
Dim hid As HiddenField
hid = e.Row.FindControl("hColor")
If (hid.Value <> Nothing) Then
e.Row.Cells(1).ForeColor = System.Drawing.ColorTranslator.FromHtml("#" + hid.Value)
End If
PS. I heart C#. Dont like VB.NET. lol :)
Have you tried:
ForeColor='<%# System.Drawing.Color.FromName(Eval("Color")) %>'
?
The reason for the error is that the runtime expects the value of ForeColor to be a color, NOT a string. The runtime cannot create "Red" from the string "Red".
You can create a function at code behind using following example to work around your issue .. function will accept string color value and will return System color. Also before populating gridview you can store the color column in the arraylist and use that arraylist to return system color. Not the best way but cant think of anything else.
ForeColor='<%# Convert.ToString(Eval("Color")) == "Blue" ? System.Drawing.Blue>
i have a datagrid which contains a lot of information in one column.
I would like a way for the user to preview some of the content in that column and then click a "more" hyperlink in that column that takes them to another page that displays all the information in that column allowing them to add more info into it and other items in that row.
I have done the more hyper link on the end of all the information but i would i change for example:
"HI my name is stefan i like walking on the beach and other stuff...more"
to
"Hi my name is stefan... more"
And how would i pull up the selected row in an new datagrid on a new asp page after they click more?
When i click the more hyperlink it doesn't open the correct row but only blogid 2.
you can use HyperLink in GridView. You can use CommangArgument and CommandName Property to Hold the specic data. When u clicked the hyperlink the data will be passed as querystring to the next page. so that you can use the querytring
And to display " ... more" you can use substring method.. substring for a legnth and concat the ....more to your hyperlink text.
MSDN for CommandArgument
Sample for Passing data with LinkButton
Handle OnRowDataBound as so:
protected void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
// say for example that the column with long text is the 5th one
e.Row.Cells[5].Text = e.Row.Cells[5].Text.Length>20?e.Row.Cells[5].Substring(0,20)+" more...";
}
}
Where the first column (Cells[0]) has the blog ID. I think that's how you have your gridview now.
I think maybe you're looking for the text-overflow: ellipsis; CSS styling. You can then use jQuery or plain javascript to toggle the style and reveal all the text.
I'm not quite sure how this is handled by Firefox today, but IE is ok.
I managed to fix this by using an sql query,
Left(content,50)
Which displays the first 50 characters.
You can try following code:
<asp:TemplateField HeaderText="Description" HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left">
<ItemTemplate>
<%# (Eval("strDescription").ToString().Length > 55 ? Eval("strDescription").ToString().Substring(0, 55) + "....." : Eval("strDescription"))%>
</ItemTemplate>
</asp:TemplateField>
and just add hyperlink in this item template and assign your blogid(in your case) to that hyperlink to open detail page that you want.
I would like to change the text of the autogenerated "select" column in an ASP.NET GridView control. The text needs to be changed to the value of a DataField.
I suspect that there is a very logical way to do this but I am missing it.
I am able to add controls and data via the pre-render event but is there an easier better way?
Use the TemplateField and place into it buttons or linkbuttons with appropriate CommandName property: ButtonField.CommandName Property
You may set this button text using DataBinder.Eval method.
The easiest way I found to do this is after the calling DataBind() just before the gridview control is displayed.
foreach (GridViewRow row in gvAgreementList.Rows)
{
LinkButton lb = (LinkButton) row.Cells[0].Controls[0];
lb.Text = "Edit";
}
after <column> write this:
<asp:CommandField ShowSelectButton="True" SelectText="Save" />
and remove AutoGenerateSelectButton="True" from Gridview attribute.
First remove auto generated select then go to GridView tasks.. top right Button of GridView and then click on commandfields -> Select then edit SelectText.
(Edited answer of ShaileshK with some changes)
Go to GridVIew tasks.. top right Button of GridView and then click on edit columns
In selected fields section Click on select field. change the value of select text.
done.
I've got an asp gridview where I want to disable the edit hyperlink field on rows where the status field is anything but "New". What logic do I need for this?
It's been a while, but I think you need to attach onto the RowDatabound event of the GridView and then access that hyperlink to disable it based on the data used for that row.
Add in a template field and simply put in this condition:
<asp:TemplateField HeaderText="Edit">
<asp:LinkButton ID="LinkButton1" Text="Edit" runat="server" Enabled='<%# Convert.ToString(Eval("Status")) == "New" ? true : false %>'></asp:LinkButton>
</asp:TemplateField>
You can use the RowDataBound method of the GridView. Within this, you can then check the value of your status field, and then either hide or disable the Hyperlink.
I used something once. It is not the best practice but it worked for me.
BTW I used it an Edit Button not in an Edit Text but i think it has to work as the same way.
In the RowDataBound Event of the GridView use this:
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Use the index of your Edit Cell
if(bool.Parse(DataBinder.Eval(e.Row.DataItem, "Status"))
{
e.Row.Cells[8].Controls.Clear();
}
}
I have GridView that allows select. It takes data from EntityDataSource. How do I get the entity Object that is selected when the row in GridView is selected ?
Primary keys of entities are not displayed in the GridView.
Thanks for answers
If you're using a template field in your gridview, you can pass the primary key in the CommandArgument property for your select command. Example:
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="btnSelect" runat="server" Text="Select" CommandName="select" CommandArgument='<%# Eval("My_Primary_Key") %>' />
</ItemTemplate>
</asp:TemplateField>
Then, when the user clicks the "select" button, this triggers a "RowCommand" event on your gridview. If you capture this event and check the e.CommandArgument property, you'll be able to gain access to the primary key corresponding to the row they selected:
protected void myGridView_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("select", StringComparison.CurrentCultureIgnoreCase))
{
int primaryKeyInteger = Convert.ToInt32(e.CommandArgument);
// Do other stuff ...
}
}
Hope this helps!
You can either use the DataKeyNames field property of a grid view or you can also put the hidden value and bind the data on data bound event. Using hidden field Gridview selected event you search find hidden filed and get the value from there. Thanks
Following link will help you.
http://forums.asp.net/t/951615.aspx
you can use hidden control in the gridview to hidden the Primary keys
you can use primary key goto the database search the record with the primary key .so then in the program use a class to receive the data.(c# is property)