带有自定义过滤器的 RadGrid
我想要实现的目标是在 RadGrid 上使用带有 DropDownList、TextBox 和 CheckBox 等控件的自定义过滤器,这些控件使用 NeedDataSource 事件来绑定数据。
我在之前的尝试中所做的是这样的:
过滤器工作正常,它使用以下代码过滤掉 RadGrid:
protected void btnSearch_Click(object sender, EventArgs e)
{
var kontakti = from k in db.Kontakt
select k;
int idTipUsera = Convert.ToInt32(rcbTipUsera.SelectedValue);
int idTvrtka = Convert.ToInt32(rcbTvrtka.SelectedValue);
if (rcbTvrtka.SelectedValue != "0")
{
kontakti = kontakti.Where(k => k.idFirma == idTvrtka);
}
if (rcbTipUsera.SelectedValue != "0")
{
kontakti = kontakti.Where(k => k.idOvlasti == idTipUsera);
}
if (chkAktivan.Checked == true)
{
kontakti = kontakti.Where(k => k.Aktivan == true);
}
else
{
kontakti = kontakti.Where(k => k.Aktivan == false);
}
int idKontakt = Convert.ToInt32(Request.QueryString["idk"]);
int idAuthKontakt = Convert.ToInt32(Session["authenticatedUI"]);
if (idKontakt > 0 && idAuthKontakt == idKontakt)
{
gvKontakti.DataSource = from k in kontakti
where k.idKontakt == idKontakt
orderby k.Prezime, k.Ime
select new { Tvrtka = k.Firma.Naziv, k.idKontakt, Naziv = k.Ime + " " + k.Prezime, Funkcija = k.Funkcija, k.Ime, k.Prezime, k.Tel1, k.Tel2, k.Mob1, k.Mob2, k.Email1, k.Email2, k.Fax, k.Adresa1, k.Adresa2, k.Adresa3, k.Grad, k.PostanskiBroj, k.Drzava, k.Biljeske, k.Aktivan, k.Username, k.Password };
}
else if (idKontakt > 0 && idAuthKontakt != idKontakt)
{
gvKontakti.DataSource = from k in kontakti
where k.idKontakt == idKontakt
orderby k.Prezime, k.Ime
select new { Tvrtka = k.Firma.Naziv, k.idKontakt, Naziv = k.Ime + " " + k.Prezime, Funkcija = k.Funkcija, k.Ime, k.Prezime, k.Tel1, k.Tel2, k.Mob1, k.Mob2, k.Email1, k.Email2, k.Fax, k.Adresa1, k.Adresa2, k.Adresa3, k.Grad, k.PostanskiBroj, k.Drzava, k.Biljeske, k.Aktivan, k.Username, k.Password };
}
else
{
gvKontakti.DataSource = from k in kontakti
orderby k.Prezime, k.Ime
select new { Tvrtka = k.Firma.Naziv, k.idKontakt, Naziv = k.Ime + " " + k.Prezime, Funkcija = k.Funkcija, k.Ime, k.Prezime, k.Tel1, k.Tel2, k.Mob1, k.Mob2, k.Email1, k.Email2, k.Fax, k.Adresa1, k.Adresa2, k.Adresa3, k.Grad, k.PostanskiBroj, k.Drzava, k.Biljeske, k.Aktivan, k.Username, k.Password };
}
gvKontakti.DataBind();
}
提到的代码的问题是,每当我尝试单击“Detalji”(实际上是标准编辑按钮)时,都会选择错误的记录。我认为问题出在“编辑”按钮中,正如我相信的那样,该按钮执行回发,导致触发 NeedDataSource 事件并加载默认数据源,然后调用先前选定行的索引。
我的问题是如何解决这个问题或者有什么替代方案?
谢谢你!
问候,
赫尔沃耶
What I'm trying to achieve is to use custom made filters with controls like DropDownList, TextBox and CheckBox on RadGrid that uses NeedDataSource event to bind the data.
What I did in my previous attempt was something like this:
Filter is working fine and it uses the following code to filter out the RadGrid:
protected void btnSearch_Click(object sender, EventArgs e)
{
var kontakti = from k in db.Kontakt
select k;
int idTipUsera = Convert.ToInt32(rcbTipUsera.SelectedValue);
int idTvrtka = Convert.ToInt32(rcbTvrtka.SelectedValue);
if (rcbTvrtka.SelectedValue != "0")
{
kontakti = kontakti.Where(k => k.idFirma == idTvrtka);
}
if (rcbTipUsera.SelectedValue != "0")
{
kontakti = kontakti.Where(k => k.idOvlasti == idTipUsera);
}
if (chkAktivan.Checked == true)
{
kontakti = kontakti.Where(k => k.Aktivan == true);
}
else
{
kontakti = kontakti.Where(k => k.Aktivan == false);
}
int idKontakt = Convert.ToInt32(Request.QueryString["idk"]);
int idAuthKontakt = Convert.ToInt32(Session["authenticatedUI"]);
if (idKontakt > 0 && idAuthKontakt == idKontakt)
{
gvKontakti.DataSource = from k in kontakti
where k.idKontakt == idKontakt
orderby k.Prezime, k.Ime
select new { Tvrtka = k.Firma.Naziv, k.idKontakt, Naziv = k.Ime + " " + k.Prezime, Funkcija = k.Funkcija, k.Ime, k.Prezime, k.Tel1, k.Tel2, k.Mob1, k.Mob2, k.Email1, k.Email2, k.Fax, k.Adresa1, k.Adresa2, k.Adresa3, k.Grad, k.PostanskiBroj, k.Drzava, k.Biljeske, k.Aktivan, k.Username, k.Password };
}
else if (idKontakt > 0 && idAuthKontakt != idKontakt)
{
gvKontakti.DataSource = from k in kontakti
where k.idKontakt == idKontakt
orderby k.Prezime, k.Ime
select new { Tvrtka = k.Firma.Naziv, k.idKontakt, Naziv = k.Ime + " " + k.Prezime, Funkcija = k.Funkcija, k.Ime, k.Prezime, k.Tel1, k.Tel2, k.Mob1, k.Mob2, k.Email1, k.Email2, k.Fax, k.Adresa1, k.Adresa2, k.Adresa3, k.Grad, k.PostanskiBroj, k.Drzava, k.Biljeske, k.Aktivan, k.Username, k.Password };
}
else
{
gvKontakti.DataSource = from k in kontakti
orderby k.Prezime, k.Ime
select new { Tvrtka = k.Firma.Naziv, k.idKontakt, Naziv = k.Ime + " " + k.Prezime, Funkcija = k.Funkcija, k.Ime, k.Prezime, k.Tel1, k.Tel2, k.Mob1, k.Mob2, k.Email1, k.Email2, k.Fax, k.Adresa1, k.Adresa2, k.Adresa3, k.Grad, k.PostanskiBroj, k.Drzava, k.Biljeske, k.Aktivan, k.Username, k.Password };
}
gvKontakti.DataBind();
}
The problem with the code mentioned is that whenever I try to click "Detalji" (which is actually standard Edit button) wrong record gets selected. I assume that problem is in Edit button which, as I believe, does the postback which causes the NeedDataSource event to get triggered and loads the default DataSource which then calls the index of the previously selected row.
My question is how to fix this or what is the alternative?
Thank you!
Regards,
Hrvoje
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,我找到了解决方案!
我所做的是使用会话,这将帮助我们稍后确定过滤后的 RadGrid 数据源是否已启动还是默认的。
之后,我必须使用 if 循环设置 PreRender 来检查前面提到的会话。
我要做的最后一件事是在重置按钮上重置会话,但这很简单。
问候,
赫尔沃耶
Ok, I've found the solution!
What I did is to use the Session which will help us to determine later if the filtered RadGrid DataSource was initiated or it was the default one.
After that I had to set PreRender with if loop to check for previously mentioned Session.
One last thing I had to do was to reset Session on reset Button but that is trivial.
Regards,
Hrvoje