Asp.net Dynamic Data 4 One-to-One ForeignKey.ascx Returning Primary Key -
i have dynamic data 4 site using linqtosql (.dbml) 2 tables one-to-one relationship. 1 table (child) not have row corresponding primary table.
whenever case (no child table row) instead of empty row cell dd foreignkey.ascx field template displaying link to:
childtable/detail.aspx?childpkfield=primarytableguid
i can't find combination of sql f-key setups and/or .dbml column property settings prevent this. solution far custom foreignkey.ascx template
protected string getdisplaystring() { object value = fieldvalue; if (value == null) { //replace //return formatfieldvalue(foreignkeycolumn.getforeignkeystring(row)); //with return ""; } else { return formatfieldvalue(foreignkeycolumn.parenttable.getdisplaystring(value)); } }
obviously not optimal solution. simplify have removed custom metadata both tables , other relationships tables involved in. no change.
i sure simple have looked @ long - appreciated!
here current create table scripts sql manager:
create table [dbo].[userprofile]( [userid] [uniqueidentifier] rowguidcol not null, [profileusername] [nvarchar](50) not null, constraint [pk_profile] primary key clustered ([userid] asc )with (pad_index = off, statistics_norecompute = off, ignore_dup_key = off, allow_row_locks = on, allow_page_locks = on) on [primary]) on [primary] textimage_on [primary] go alter table [dbo].[userprofile] add constraint [df_profile_userid] default (newid()) [userid] go
and
create table [dbo].[subscribeduser]( [subuserid] [uniqueidentifier] not null, [subexpiredate] [date] null, constraint [pk_subscribedusers] primary key clustered ([subuserid] asc) (pad_index = off, statistics_norecompute = off, ignore_dup_key = off, allow_row_locks = on, allow_page_locks = on) on [primary]) on [primary] go alter table [dbo].[subscribeduser] check add constraint [fk_subscribeduser_userprofile] foreign key([subuserid]) references [dbo].[userprofile] ([userid]) go alter table [dbo].[subscribeduser] check constraint [fk_subscribeduser_userprofile] go
in fact, first method used perhaps optimal solution query has been made on server-side determine whether column data matches foreign key relationship. no more querying needed, test fieldvalue == null
. besides, template controls there edited, why they're not compiled assemblies:
protected string getdisplaystring() { object value = fieldvalue; if (value == null) { //replace //return formatfieldvalue(foreignkeycolumn.getforeignkeystring(row)); //with return ""; } else { return formatfieldvalue(foreignkeycolumn.parenttable.getdisplaystring(value)); } }
however, if intend preserved value of source field, use method instead:
protected string getnavigateurl() { //replace //if (!allownavigation) //with if (!allownavigation || fieldvalue == null) { return null; } if (string.isnullorempty(navigateurl)) { return foreignkeypath; } else { return buildforeignkeypath(navigateurl); } }
under conditions (including default css styles), generate hyperlink-looking field cannot clicked. can use jquery filter , remove false hyperlinks setting outerhtml
innerhtml
(client-side scripting). of course, there more elegant methods adding <asp:label>
foreignkey.ascx , switching control when needed.
note: method works when using ado.net entity data model. didn't try on linqtosqlclasses.
Comments
Post a Comment