I have a database with link urls and their respective display texts. I need to check if they are broken or not before their display string is shown in the gridview.
I am using SqlDatasource, is there a way to process records and use custom HTML markup to show them while using the SqlDataSource?
I am trying to use the OnSelected event of SqlDatasource but cant get how to use it.
I believe what you are trying to do is make sure that the hyperlink is valid before it gets put into the datagrid. To do this, you would need to subscribe to the RowDataBound event on your grid. From there, you can run code to evaluate your URL. Here is a quick example that would check to be sure that the URL field is not an empty string:
protected void selectedBookList_RowDataBound(object sender, GridViewRowEventArgs e)
{
if ((e.Row != null) && (e.Row.RowType == DataControlRowType.DataRow))
{
string test = DataBinder.Eval(e.Row.DataItem, "URL").ToString();
if (test.Length == 0)
{
e.Row.Cells[3].Visible = false;
}
else
{
e.Row.Cells[3].Visible = true;
}
}
}
Instead of testing to be sure the length is equal to zero, you could check to see if the link is dead or not. Once you have evaluated it, you can hide the cell like I am doing here or you could modify the link, put in a generic link, etc.
Related
The AutoCompleteSource and AutoCompleteMode properties of the TextBox allow me to use automatic completion in textboxes.
I have bound directly a datatable as AutoCompleteSource of the textbox and it works well.
In some situations that the input words is not available in the sources, the auto completion has no result and so, i need to do something else in those situations.
How should i check whether the automatic completion result is empty?
Here is one approach you can take. The following code will get suggestions in the TextChanged event of the textbox when more than 3 characters have been entered. We go get the suggestions and then check if any suggestions were returned. If yes, we set the AutoCompleteCustomSource. Otherwise, we will do something--whatever we want to do.
private void textBox1_TextChanged(object sender, EventArgs e)
{
TextBox t = sender as TextBox;
if (t != null)
{
// Here I am making the assumption we will get suggestions after
// 3 characters are entered
if (t.Text.Length >= 3)
{
// This will get the suggestions from some place like db,
// table etc.
string[] arr = GetSuggestions(t.Text);
if (arr.Length == 0) {// do whatever you want to}
else
{
var collection = new AutoCompleteStringCollection();
collection.AddRange(arr);
this.textBox1.AutoCompleteCustomSource = collection;
}
}
}
}
I recently installed syncfusion(Previously using telerik) and am trying to switch out the the RadGridView for the GridDataBoundGrid and my function doesnt seem to work the same as it did
private void OnColumnHeaderMouseClick(object sender, ColumnClickEventHandler e) {
if (e.Row is GridViewTableHeaderRowInfo) {
int index = tabControl1.SelectedIndex;
EditHeader eh = new EditHeader(this.UpdateHeader);
eh.TextBox1.Text = ds.Tables[index.ToString()].Columns[e.ColumnIndex].ToString();
eh.TextBox2.Text = e.ColumnIndex.ToString();
eh.Show();
}
}
e.Row Doesnt exist GridViewTableHeaderRowInfo Doesnt exist and e.ColumnIndex Doesnt exist
The point of this is to open another part of the application when a column header is clicked and im not sure if im gonna have to rewrite the function from scratch or not.
So are there any events that would work directly with my function or do I have to write a workaround
By default, the CellClick event will trigger while the clicking on table cells. The CellType property can be used to check whether the CurrentCell is column header cell or not and the ColIndex property can be used to get the current column index. Please make use of code and sample,
Code snippet
//Event Triggering
this.gridDataBoundGrid1.CellClick += GridDataBoundGrid1_CellClick;
//Event handling
private void GridDataBoundGrid1_CellClick(object sender, GridCellClickEventArgs e)
{
//To Check for column header cell
if(this.gridDataBoundGrid1[e.RowIndex, e.ColIndex].CellType == "ColumnHeaderCell")
{
//Your code show the another part of the project.
}
}
Note
Please make use of below KB to get the particular record/row based on index.
KB link: https://www.syncfusion.com/kb/5953/how-to-get-the-row-data-from-griddataboundgrid
Sample link: https://drive.google.com/file/d/0Bwk9nUFE3rlxR0xZMXRhU0xqdTA/view?usp=sharing
Regards,
Arulpriya
I have the following code:
protected void gv_AfterPerformCallback(object sender, ASPxGridViewAfterPerformCallbackEventArgs e)
{
if(e.CallbackName == "UPDATEEDIT")
{
ASPxGridView gv = (ASPxGridView)sender;
GridViewDataColumn col = gv.Columns[0] as GridViewDataColumn;
ASPxTextBox txt = gv.FindEditRowCellTemplateControl(col, "test") as ASPxTextBox;
}
}
In this code, gv is defined and it finds col. However, when running the FindEditRowCellTemplateControl part, it returns null for the textbox. The textbox definitely exists, I know this as this code used to work and hasn't been touched, but also if I change the if statement to look for e.CallbackName == "CANCELEDIT", the code works as intended. There are other calls to other controls within this same block and none of them are returning valid controls.
The only thing that has changed between working code and not working, is that I changed the BackColor of another control in another column in the same grid. I have since changed it back as I wondered if this, inexplicably, was the issue.
What can have happened? Why would txt be null when e.CallbackName == "UPDATEEDIT" but is initialised correctly when e.CallbackName == "CANCELEDIT"?
I am using ASP.NET and C# on .NET 4 to develop a simple app. I have a repeater with an item template containing a few controls; one of them is a label that must be set depending on a complex calculation. I am using the OnItemDataBound event to compute the text and set the label's text in the code behind, like this:
protected void repRunResults_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
//capture current context.
Repeater repRunResults = (Repeater)sender;
Label laMessage = (Label)repRunResults.Controls[0].FindControl("laMessage");
DSScatterData.RunResultsRow rRunResults = (DSScatterData.RunResultsRow)((DataRowView)(e.Item.DataItem)).Row;
//show message if needed.
int iTotal = this.GetTotal(m_eStatus, rRunResults.MaxIterations, rRunResults.TargetLimit);
if(iTotal == 100)
{
laMessage.Text = "The computed total is 100.";
}
else
{
laMessage.Text = "The computed total is NOT 100.";
}
}
The data source for my repeater contains a few rows, so I would expect that each impression of the repeater would call the event handler and show the message according to the data in the associated row. However, I only get one message, which appears on the first repeater impression but matches the data for the last row in the data source.
It seems like every time the ItemDataBound event fires, the controls that my code captures are the same ones, so that I overwrite the message on every impression of the repeater. I have stepped through the code and this is what it is apparently happening.
Any idea why? And how to fix it?
Note. My repeater is nested inside another repeater. I don't think this should be relevant, but it might be.
You are grabbing the first one. You need to use the item that is being passed in like so:
protected void repRunResults_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
//capture current context.
Repeater repRunResults = (Repeater)sender;
Label laMessage = e.Item.FindControl("laMessage"); //<-- Used e.Item here
DSScatterData.RunResultsRow rRunResults = (DSScatterData.RunResultsRow)((DataRowView)(e.Item.DataItem)).Row;
//show message if needed.
int iTotal = this.GetTotal(m_eStatus, rRunResults.MaxIterations, rRunResults.TargetLimit);
if(iTotal == 100)
{
laMessage.Text = "The computed total is 100.";
}
else
{
laMessage.Text = "The computed total is NOT 100.";
}
}
I am checking some condition in the GridView_RowCommand event. Based on the Condition I want to set SkinID or remove SkinID for the TextBox.
This is my Code:
if (dt.Rows[0]["D1Variable"].ToString() == "0")
{
txtMRVD1.Text = dt.Rows[0]["D1"].ToString();
txtMRVD1.ReadOnly = true;
txtMRVD1.SkinID = "txtreadonly";
}
else
{
txtMRVD1.Text = "";
txtMRVD1.ReadOnly = false;
}
But it throws the following error.
"The 'SkinId' property can only be set in or before the Page_PreInit event for static controls. For dynamic controls, set the property before adding it to the Controls collection."
How to solve this problem?
I would say, based on the error message, that you simply need to move that code to the Page_PreInit event.
However, as MSDN says: "If the request is a postback, the values of the controls have not yet been restored from view state. If you set a control property at this stage, its value might be overwritten in the next event."
You may need to get more creative in how you approach this - perhaps storing the information used to determine the skin id in Session. I suggest getting familiar, if you're not already, with the ASP.NET Page Life Cycle.
Here's an example that might work for you, or at least get you pointed in the right direction:
protected void Page_PreInit(object sender, EventArgs e)
{
if ((string)Session["D1Variable"] == "0")
{
txtMRVD1.SkinID = "txtreadonly";
}
}
You can then do the rest of your logic in the GridView RowCommand event.